Python中列表常用操作及函数
作用:类似其他语言的数组 (数字的组合,字母的组合,字符串的组合)
符号:[]
# 元素的获取使用(下标-索引)
names = ['Jack','lucy','Tom','superman','iron man']
print(names[0])
print(names[1])
print(names[-1])
Jack
lucy
iron man
可结合循环使用,支持遍历:
for i in names:
print(i)
Jack
lucy
Tom
superman
iron man
# 查询iron man,如果有, 打印并结束
for name in names:
if name == 'iron man':
print('iron man在里面')
break
else:
print('没有找到iron man')
iron man在里面
brands = ['hp','dell','thinkpad','lenvo','mac','神州']
print(brands)
print(brands[-1])
brands[-1]='HASEE'
print(brands)
[‘hp’, ‘dell’, ‘thinkpad’, ‘lenvo’, ‘mac’, ‘神州’]
神州
[‘hp’, ‘dell’, ‘thinkpad’, ‘lenvo’, ‘mac’, ‘HASEE’]
# 替换列表中的元素
brands = ['hp','dell','thinkpad','lenvo','mac','华为']
for i in range(len(brands)):
if '华为' in brands[i]:
brands[i] = 'HUAWEI'
break
print(brands)
[‘hp’, ‘dell’, ‘thinkpad’, ‘lenvo’, ‘mac’, ‘HUAWEI’]
列表中元素的删除:
del brands[2]
print(brands)
[‘hp’, ‘dell’, ‘lenvo’, ‘mac’, ‘HUAWEI’]
#只要是hp,mac都删除
brands = ['hp','dell','thinkpad','lenvo','mac','神州']
l = len(brands)
i = 0
while i < l:
if 'hp' in brands[i] or 'mac' in brands[i]:
del brands[i]
l -= 1
continue
i += 1
print(brands)
[‘dell’, ‘thinkpad’, ‘lenvo’, ‘神州’]
#in可以应用在字符串判断中,也可以应用于列表
i = 1
for w in ['goods','good','abc','aaaa']:
print('good' in w)
print('-----',i)
i += 1
True
----- 1
True
----- 2
False
----- 3
False
----- 4
列表中允许存放任意类型的值:
list1 = ['杨超越','佟丽娅','赵丽颖','刘亦菲','迪丽热巴',1000,99.9]
print(list1)
[‘杨超越’, ‘佟丽娅’, ‘赵丽颖’, ‘刘亦菲’, ‘迪丽热巴’, 1000, 99.9]
#列表支持切片,将截取的结果再次保存在一个列表中
print(list1[2:5])
print(list1[-1:])
print(list1[-3:-1])
print(list1[::2])
[‘赵丽颖’, ‘刘亦菲’, ‘迪丽热巴’]
[99.9]
[‘迪丽热巴’, 1000]
[‘杨超越’, ‘赵丽颖’, ‘迪丽热巴’, 99.9]
# 反向切片
list1 = ['杨超越','佟丽娅','赵丽颖','刘亦菲','迪丽热巴',1000,99.9]
print(list1[::-1])
print(list1[-1::-1])
[99.9, 1000, ‘迪丽热巴’, ‘刘亦菲’, ‘赵丽颖’, ‘佟丽娅’, ‘杨超越’]
[99.9, 1000, ‘迪丽热巴’, ‘刘亦菲’, ‘赵丽颖’, ‘佟丽娅’, ‘杨超越’]
列表元素的添加:
append() 末尾追加
extends() 列表合并
insert() 指定位置插入
girls = []
while True:
name = input('请输入心中女神的名字:')
if name == 'quit'
break
girls.append(name)
print(girls)
list1=['aa','bb','cc']
list2=['cc','dd','ee']
list1.extend(list2)
print(list1)
[‘aa’, ‘bb’, ‘cc’, ‘cc’, ‘dd’, ‘ee’]
# 符号“+”也可以用于列表的合并
list1=['aa','bb','cc']
list2=['cc','dd','ee']
list1+list2
[‘aa’, ‘bb’, ‘cc’, ‘cc’, ‘dd’, ‘ee’]
girls = ['杨幂','黑嘉嘉',"孙俪",'巩俐','王丽坤']
girls.insert(1,'刘涛')
print(girls)
[‘杨幂’, ‘刘涛’, ‘黑嘉嘉’, ‘孙俪’, ‘巩俐’, ‘王丽坤’]
#案例:产生10个不同的随机数,将其保存到列表中,并找出列表中的最大值。
import random
random_list = []
i = 0
while i <10:
ran = random.randint(1,50)
if ran not in random_list:
random_list.append(ran)
i += 1
print(random_list)
print(max(random_list))
[7, 18, 8, 33, 29, 15, 45, 9, 49, 26]
49
排序函数 sorted() -->默认升序
new_list = sorted(random_list)
print(new_list)
new_list = sorted(random_list,reverse=True)
print(new_list)
[4, 5, 9, 11, 13, 19, 22, 23, 35, 47]
[47, 35, 23, 22, 19, 13, 11, 9, 5, 4]
#列表中的元素可以是列表
l5 = [[1,2],[3,2,1],[4,5]]
print(len(l5))
print(l5[1])
print(l5[1][1])
3
[3, 2, 1]
2
list() 将指定内容转换为列表,list中存放的内容应为可迭代的
print(range(1,10,3))
print(list(range(1,10,3)))
range(1, 10, 3)
[1, 4, 7]
s = 'abc'
print(list(s))
[‘a’, ‘b’, ‘c’]
pop() 弹栈 —>移除最后一个元素,默认不传参
hotpot_list = ['海底捞','呷哺呷哺','热辣一号','宽板凳']
hotpot_list.append('张亮麻辣烫')
print(hotpot_list)
[‘海底捞’, ‘呷哺呷哺’, ‘热辣一号’, ‘宽板凳’, ‘张亮麻辣烫’]