1、格式
[数据1,数据2,数据3,...]
列表可以一次性存多个数据,可以为不同的数据类型
2、下标
从0开始循序向下分配
3、常用函数
查找 |
name=['tom','lili','rode']
print(name.count('lili')) #1
print(len(name))
|
判断是否存在 |
print('lili' in name) #True
|
插入 |
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']
|
删除 |
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']
|
修改 |
(P.S:reverse是在True降序,False升序(默认)) |
复制 |
|
遍历 |
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]) # 李四
|