1、格式

[数据1,数据2,数据3,...]

列表可以一次性存多个数据,可以为不同的数据类型

2、下标

从0开始循序向下分配

3、常用函数

 

查找
  • index():返回指定数据所在位置下标,不存在就报错
  • count():返回某个字串在字符串中出现的次数
  • len():返回列表列表中的数据个数
name=['tom','lili','rode']
print(name.count('lili')) #1
print(len(name))
判断是否存在
  • in/not in:返回True/False
print('lili' in name)   #True
插入
  • append():列表结尾追加数据,如果数据是一个列表,则追加整个序列到列表中
  • extend():列表结尾增加数据,如果数据是一个列表,则将这个序列的数据拆开再逐一添加到列表
  • insert():指定位置增加数据
name.append([11,22])  # ['tom', 'lili', 'rode', [11, 22]]
name=[name=['tom','lili','rode']]
name.extend('xiaomi')#['tom', 'lili', 'rode', 'x', 'i', 'a', 'o','m','i']
str1=['123','aaa']
str1.insert(1,'bbb') # ['123','bbb','aaa']
 删除
  •  del:  删除目标,列表删了就没了
  • pop(): 删除并返回指定位置上的元素(默认为最后一个)  pop(index))
  • remove():删除的第一个匹配项
  • clear() :清空列表(列表还存在)
fruit = ['apple', 'peach', 'banana']
del fruit[2]
print(fruit)  # ['apple', 'peach']
del fruit
#print(fruit)  # 报错,fruit已经被删掉了,不存在了

fruit = ['apple', 'peach', 'banana']
print(fruit.pop(1))  # peach
print(fruit)  # ['apple', 'banana']
fruit.remove('banana')
print(fruit)  # ['apple']
 修改
  • 直接指定下标修改:eg:fruit[1]='watermalen'
  • 逆序:列表序列.reverse()
  • 排序:列表序列.sort(key=None,reverse=False)

                  (P.S:reverse是在True降序,False升序(默认))

 复制
  • copy()
遍历
fruit = ['apple', 'peach', 'banana']
i=0
#while
while i< len(fruit):
    print(fruit[i])
    i+=1 # 注:python里面没有i++

#for
for i in fruit:
    print(i)
嵌套

         列表可以套子列表

name=[['张三','李四','王五'],['张龙','赵虎']]
print(name[0])  # ['张三', '李四', '王五']
print(name[0][1])  # 李四 
posted on 2020-02-27 21:29  孤岛蓝鲸  阅读(226)  评论(0编辑  收藏  举报