Python 列表07
2021-06-09 00:02
标签:获取 布尔 客服 bsp 找不到 字符串 文化 有序 data 是一个有序且可变的容器,在里面可以存放多个不同类型的元素 append() extend() insert() remove() pop() clear() index() 找不到会报错 sort() reverse() 不是排序,而是将列表反向排列 相加 相乘 运算符in包含 获取长度 索引 切片 步长 for循环 转换 嵌套 Python 列表07 标签:获取 布尔 客服 bsp 找不到 字符串 文化 有序 data 原文地址:https://www.cnblogs.com/tonitaka/p/14500462.html列表(list)
list = [‘阿斯顿‘,‘阿发师‘,‘收发室‘]
list = [98,88,66,-1]
list = [1,True,‘Alex‘,‘阿萨德‘]
不可变类型:字符串、布尔类型、整型
name = ‘ccc‘
data = name.upper()
print(name) #ccc
print(data) #CCC
可变类型:列表(内部数据元素可以修改)
list = [‘车子‘,‘妹子‘,‘小子‘]
list.append(‘大舅子‘)
print(list) #[‘车子‘, ‘妹子‘, ‘小子‘, ‘大舅子‘]
tools = [‘搬砖‘,‘ 菜刀‘,‘锄头‘]
tools.extend([11,22,33])
print(tools) #[‘搬砖‘, ‘ 菜刀‘, ‘锄头‘, 11, 22, 33]
list = [‘aaa‘,‘bbb‘,‘ccc‘]
list.insert(0,‘ddd‘)
list.insert(2,‘eee‘)
print(list) #[‘ddd‘, ‘aaa‘, ‘eee‘, ‘bbb‘, ‘ccc‘]
list = [‘aaa‘,‘bbb‘,‘ccc‘,‘ddd‘]
list.remove(‘ccc‘)
print(list) #[‘aaa‘, ‘bbb‘, ‘ddd‘]
list = [‘aaa‘,‘bbb‘,‘ccc‘,‘ddd‘]
list.pop()
print(list) #[‘aaa‘, ‘bbb‘, ‘ccc‘]
#当pop不指定索引位置,默认是删除最后一个
list = [‘aaa‘,‘bbb‘,‘ccc‘,‘ddd‘]
list.pop(1)
print(list) #[‘aaa‘, ‘ccc‘, ‘ddd‘]
#当给pop指定索引时,则删除指定索引
list = [‘aaa‘,‘bbb‘,‘ccc‘,‘ddd‘]
list.clear()
print(list) #[]
list = [‘aaa‘,‘bbb‘,‘ccc‘,‘ddd‘]
index = list.index(‘ccc‘)
print(index) #2
list = [2,6,8,45,8,3,9,5,1]
list.sort()
print(list) #[1, 2, 3, 5, 6, 8, 8, 9, 45]
list = [2,6,8,45,8,3,9,5,1]
list.sort(reverse=True)
print(list) #[45, 9, 8, 8, 6, 5, 3, 2, 1]
#reverse为True,则是反向排序
list = [‘阿斯顿‘,‘搭嘎上网‘,‘噶文化馆‘,‘高科技为i‘,‘一会就好你‘]
list.sort()
print(list) #[‘一会就好你‘, ‘噶文化馆‘, ‘搭嘎上网‘, ‘阿斯顿‘, ‘高科技为i‘]
#字符串是根据码点来排序的
list = [2,6,8,45,8,3,9,5,1]
list.reverse()
print(list) #[1, 5, 9, 3, 8, 45, 8, 6, 2]
其他功能
list = [‘阿斯顿‘,‘阿萨德‘] + [‘请问‘,123]
print(list) #[‘阿斯顿‘, ‘阿萨德‘, ‘请问‘, 123]
list = [‘阿斯顿‘,‘阿萨德‘] * 2
print(list) #[‘阿斯顿‘, ‘阿萨德‘, ‘阿斯顿‘, ‘阿萨德‘]
list = [‘阿斯顿‘,‘阿萨德‘,‘沙雕‘,‘安蓓乐‘]
name = ‘沙雕‘
if name in list:
print(‘%s在里面‘ % name)
else:
print(‘%s不在里面‘ % name)
#沙雕在里面
list = [‘阿斯顿‘,‘阿萨德‘,‘沙雕‘,‘安蓓乐‘]
l = len(list)
print(l) #4
#读
list = [‘阿斯顿‘,‘阿萨德‘,‘沙雕‘,‘安蓓乐‘]
print(list[0]) #阿斯顿
print(list[1]) #阿萨德
print(list[3]) #安蓓乐
#改
list = [‘阿斯顿‘,‘阿萨德‘,‘沙雕‘,‘安蓓乐‘]
list[0] = ‘法拉分开‘
print(list) #[‘法拉分开‘, ‘阿萨德‘, ‘沙雕‘, ‘安蓓乐‘]
#删
list = [‘阿斯顿‘,‘阿萨德‘,‘沙雕‘,‘安蓓乐‘]
del list[2]
print(list) #[‘阿斯顿‘, ‘阿萨德‘, ‘安蓓乐‘]
#读
list = [‘草‘,‘阿萨‘,‘倒萨‘,‘师傅‘]
print(list[0:2]) #[‘草‘, ‘阿萨‘]
print(list[1:]) #[‘阿萨‘, ‘倒萨‘, ‘师傅‘]
print(list[:-1]) #[‘草‘, ‘阿萨‘, ‘倒萨‘]
#改
list = [‘草‘,‘阿萨‘,‘倒萨‘,‘师傅‘]
list[0:2] = [11,55,33]
print(list) #[11, 55, 33, ‘倒萨‘, ‘师傅‘]
list = [‘草‘,‘阿萨‘,‘倒萨‘,‘师傅‘]
list[2:] = [11,55,33]
print(list) #[‘草‘, ‘阿萨‘, 11, 55, 33]
list = [‘草‘,‘阿萨‘,‘倒萨‘,‘师傅‘]
list[4:] = [11,55,33]
print(list) #[‘草‘, ‘阿萨‘, ‘倒萨‘, ‘师傅‘, 11, 55, 33]
#删
list = [‘草‘,‘阿萨‘,‘倒萨‘,‘师傅‘]
del list[1:]
print(list) #[‘草‘]
list = [‘草‘,‘阿萨‘,‘倒萨‘,‘师傅‘,‘奥斯卡件打蜡卡‘]
print(list[1:4:2]) #[‘阿萨‘, ‘师傅‘]
print(list[0::2]) #[‘草‘, ‘倒萨‘, ‘奥斯卡件打蜡卡‘]
print(list[1::2]) #[‘阿萨‘, ‘师傅‘]
print(list[4:1:-1]) #[‘奥斯卡件打蜡卡‘, ‘师傅‘, ‘倒萨‘]
list = [‘草‘,‘阿萨‘,‘倒萨‘,‘师傅‘,‘奥斯卡件打蜡卡‘]
print(list[::-1]) #[‘奥斯卡件打蜡卡‘, ‘师傅‘, ‘倒萨‘, ‘阿萨‘, ‘草‘]
list = [‘草‘,‘阿萨‘,‘倒萨‘,‘师傅‘,‘奥斯卡件打蜡卡‘]
for item in list:
print(item)
list = [‘刘达‘,‘阿萨‘,‘刘希‘,‘刘张欢问客服‘,‘奥斯卡件打蜡卡‘,‘送大小‘,‘刘能‘]
for item in list:
if item.startswith(‘刘‘):
list.remove(item)
print(list) #[‘阿萨‘, ‘刘张欢问客服‘, ‘奥斯卡件打蜡卡‘, ‘送大小‘]
list = [‘刘达‘,‘阿萨‘,‘刘希‘,‘刘张欢问客服‘,‘奥斯卡件打蜡卡‘,‘送大小‘,‘刘能‘]
for index in range(len(list) - 1,-1,-1):
item = list[index]
if item.startswith(‘刘‘):
list.remove(item)
print(list) #[‘阿萨‘, ‘奥斯卡件打蜡卡‘, ‘送大小‘]
name = ‘下屁孩‘
data = list(name)
print(data)
#元组可以转换为列表
name = (11,‘下屁孩‘,22,33)
data = list(name)
print(data) #[11, ‘下屁孩‘, 22, 33]
data = [‘谢谢你‘,[‘海燕‘,‘少贵‘],True,[44,55,[11,22,33],88],‘送大饼‘]
print(data[0]) #谢谢你
print(data[1]) #[‘海燕‘, ‘少贵‘]
print(data[0][2]) #你
print(data[1][-1]) #少贵
data.append(666)
print(data) #[‘谢谢你‘, [‘海燕‘, ‘少贵‘], True, [44, 55, [11, 22, 33], 88], ‘送大饼‘, 666]
data[1].append(‘不予‘)
print(data) #[‘谢谢你‘, [‘海燕‘, ‘少贵‘, ‘不予‘], True, [44, 55, [11, 22, 33], 88], ‘送大饼‘, 666]
del data[-2]
print(data) #[‘谢谢你‘, [‘海燕‘, ‘少贵‘, ‘不予‘], True, [44, 55, [11, 22, 33], 88], 666]
data[-2][1] = ‘斯柯达‘
print(data) #[‘谢谢你‘, [‘海燕‘, ‘少贵‘, ‘不予‘], True, [44, ‘斯柯达‘, [11, 22, 33], 88], 666]