1、格式 [數據1,數據2,數據3,...] 列表可以一次性存多個數據,可以為不同的數據類型 2、下標 從0開始循序向下分配 3、常用函數 查找 index():返回指定數據所在位置下標,不存在就報錯 count():返回某個字串在字元串中出現的次數 len():返回列表列表中的數據個數 name= ...
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]) # 李四
|