第10天 列表的

这篇博客详细介绍了Python列表的相关操作,包括如何在列表中处理随机数、查找最大值和最小值、计算列表总和、进行列表排序以及讨论了其他实用操作,并给出了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

本文主要介绍列表的使用规范


提示:以下是本篇文章正文内容,下面案例可供参考

一、列表中与随机数

'''
#产生10个随机数,将其保存到列表中

步骤:
1.如何产生随机数
2.10个数字产生
3.将产生的随机数放到列表中
4.打印列表


'''

import random
'''
random_list=[] #用来存放随机数
for i  in range(10):
	ran=random.randint(1,50)
	#保存到列表
	random_list.append(ran)
print(random_list)

'''
#产生10个不同的随机数,将其保存到列表中
random_list=[]
for i in range(10):
	ran = random.randint(1,20)
	if  ran not in random_list:
		random_list.append(ran)
print(random_list)  #个数不一定是10个


#while循环
random_list=[]
i=0
while i<10:
	ran=random.randint(1,20)
	if ran not in random_listn:
		random_list.append(ran)
		i+=1
print(random_list)

二 , 求列表中的最大值和最小值

#找出列表中的最大值--->max(list)
max_value = max(random_list)
print(max_value)

#方式二通过for循环去寻找
b=0
for i in random_list:
	if b<=i:
		b=i
print(b)

#方式三
#假设列表中的第一个元素就是最大值
mvalue=random_list[0]
minvalue=random_list[0]
for value in random_list:
	#求最小值
	if  value=mvalue:
		mvalue=value
	#求最小值
	if value<minvalue:
		minvalue=value

print('最大值是:',mvalue,'最小值是',minvalue)

三、列表求和

#求和
he= sum(random_list)
print('系统计算求和:',he)

sum_1=0
for  i in random_list:
 	sum_1+=i
 print(sum)

四,列表排序

代码如下(示例):

# 排序 sorted 排序 默认是升序

#sorted(list.reverse=True) ------->降序 
new_list = sorted(random_list,reverse=True)
print(new_list)

#自己写排序方法 冒泡排序

五,列表的其他

代码如下(示例):

'''
字符串中可以使用的符号:
+
*
im
not in
not is
[]

'''
'''
列表支持的符号:
+
*
'''

l1=[1,2,3]
l2=[5,6,7]
l3=l1+l2
print(l3)

l4=[5,6]*3
print(l4)

result = 3 not in [1,2,3,4]
print(result)

result = [3] in [1,2,3,4,5]
print(result)

[1,2,3,'aa','bb'.[1,2],[6,8,9,0]]

[[1,2],[6,88,8,9]]  #二位列表

`

该处使用的url网络请求的数据。


'''
列表中的元素:
整型
字符串类型
浮点型
列表
元组
字典
对象

[[],[],[]]
'''

result= [3,2] in [1,2,[3,2,1],4,5]
print(result)


l5=[[1,2],[3,2,1],[4,5],[7,3,1]]
print(len(l5)) #3

e = l5[2]
print(e,type(e)) #[4,5]

print(e[1])

print(l5[1][1])

print(l5[1][2])


# list(range(1,10,3))

print(range(1,10,3))
'''

类型转换
str()
int()
list() 指定的内容转成列表

s='abc'
result = list(s)  #['a','b','c']
'''

print(list(range(1,10,3))) # []

print(list(range(10,1,-3)))

s='abc'
result=list(s)
print(result)


result=list(10) # int obj is not iterablle
print(result)

# iterable 可迭代的 for --- in 里面可以循环就是可迭代的
'''
'abcdef'---> a b c
for i in range (5):
	pass

for i in 10 :

'''
print(list(range(5)))
# print(int(list[4,5])) #无法进行类型转换

六 列表的函数总结:

#列表的函数:
'''

字符串函数:
'abc'.split('-')

列表函数:只有通过列表才可以调用的函数

添加: append extend  insert
删除 del list[index]
    remove(e) 删除列表中第一次出现的元素e,返回值是None
              如果没有找到删除的元素则出现异常
    pop() : 弹栈 移除列表中的最后一个元素,返回值是删除的那个元素
            默认是删除最后一个,但是也可以指定index(下标)删除
    clear()
'''
'''
翻转:
resver()
排序:
  sort()

次数:
   count()
游戏:
'''

hotpot_list = ['海底捞','热辣一号','快攀登','张亮麻辣烫']
hotpot_list.append('张亮麻辣烫')

print(hotpot_list)
'''
result=hotpot_list.remove('张亮麻辣烫')

print(result)

print(hotpot_list)
'''

result =hotpot_list.pop()
print(result)
print(hotpot_list)

result= hotpot_list.pop(2)
print(hotpot_list)

'''
result=hotpot_list.clear()
print(result)
print(hotpot_list)
'''

hotpot_list.reverse()
print(hotpot_list)

print(hotpot_list[::-1]) #只是逆序拿出列表的元素,打印出来
print(hotpot_list)


hotpot_list.reverse()  # 改变了列表的位置结构
print(hotpot_list)

#系统提供的排序
'''
sorted(list)
list.sort()

'''

l=[4,8,1,8,9,5,7]

l.sort(reverse=True) #升序  反序

print(1)

print(l.count(8))

总结

本文是对列表的延展。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值