Python 列表07

2021-06-09 00:02

阅读:336

标签:获取   布尔   客服   bsp   找不到   字符串   文化   有序   data   

列表(list)

  是一个有序且的容器,在里面可以存放多个不同类型的元素

list = [阿斯顿,阿发师,收发室]
list = [98,88,66,-1]
list = [1,True,Alex,阿萨德]

  不可变类型:字符串、布尔类型、整型

name = ccc
data = name.upper()
print(name)    #ccc        
print(data)    #CCC

  可变类型:列表(内部数据元素可以修改)

  append()

list = [车子,妹子,小子]
list.append(大舅子)
print(list)    #[‘车子‘, ‘妹子‘, ‘小子‘, ‘大舅子‘]

  extend()

tools = [搬砖, 菜刀,锄头]
tools.extend([11,22,33])
print(tools)    #[‘搬砖‘, ‘ 菜刀‘, ‘锄头‘, 11, 22, 33]

   insert()

list = [aaa,bbb,ccc]
list.insert(0,ddd)
list.insert(2,eee)
print(list)    #[‘ddd‘, ‘aaa‘, ‘eee‘, ‘bbb‘, ‘ccc‘]

  remove()

list = [aaa,bbb,ccc,ddd]
list.remove(ccc)
print(list)     #[‘aaa‘, ‘bbb‘, ‘ddd‘]

  pop()

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指定索引时,则删除指定索引

  clear()

list = [aaa,bbb,ccc,ddd]
list.clear()
print(list)    #[]

  index()

  找不到会报错

list = [aaa,bbb,ccc,ddd]
index = list.index(ccc)
print(index)    #2

  sort()

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‘]
#字符串是根据码点来排序的

  reverse()

  不是排序,而是将列表反向排列

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)    #[‘阿斯顿‘, ‘阿萨德‘, ‘阿斯顿‘, ‘阿萨德‘]

  运算符in包含

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])    #[‘奥斯卡件打蜡卡‘, ‘师傅‘, ‘倒萨‘, ‘阿萨‘, ‘草‘]

  for循环

list = [,阿萨,倒萨,师傅,奥斯卡件打蜡卡]
for item in list:
    print(item)
  • 一道踩坑题
  • 题目:删除列表list里面姓刘的元素
  • 切记:不要在列表循环里面删除元素

 

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)    #[‘阿萨‘, ‘奥斯卡件打蜡卡‘, ‘送大小‘]

  转换

  • int,bool无法转换成列表
  • str可以转换
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]

 

Python 列表07

标签:获取   布尔   客服   bsp   找不到   字符串   文化   有序   data   

原文地址:https://www.cnblogs.com/tonitaka/p/14500462.html


评论


亲,登录后才可以留言!