提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
本文主要介绍列表的使用规范
提示:以下是本篇文章正文内容,下面案例可供参考
一、列表中与随机数
'''
#产生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))
总结
本文是对列表的延展。