list
li = [111,222]
# 背後的操作就是
li = list([111,222])
# 將字符串轉成列表類型
x = "aasdasd"
li = list(x)
print(li) #['a', 'a', 's', 'd', 'a', 's', 'd']
# 將字典轉,轉的是字典的"key"
# 工作原理
li = []
x = "aasdasd"
for i in x:
li.append(i)
print(li)
l=['a','b','c']
# 正向
print(l[1]) #b
# 反向
print(l[-1]) #c
# 改值, 改大小寫都行
l[2]='d'
print(l) #['a', 'b', 'd']
# 如果索引不存在則報錯
l[8]='d'
print(l) #報錯
li=['song','hai','xing','hhh','lihai']
# 顧頭不顧尾顧頭不顧尾、步長
print(li[0:3]) # ['song','hai','xing']
print(li[0:5:2]) # ['song','xing','lihai']
# 全部切片(淺拷貝)
print(li[:len(li)])
print(li[0:])
print(li[:])
# 顛倒列表
print(li[::-1]) # ['lihai', 'hhh', 'xing', 'hai', 'song']
ps : 深淺拷貝https://www.cnblogs.com/songhaixing/p/14015669.html
li=['song','hai','xing','hhh','lihai']
print(len(l)) #5
li = [111,222,333,[344,33]]
print(111 in l) #True
print(099 in l) #False
l=["aaa",2222,"cccc"]
res=l.append("ddd")
l.append("eee")
print(l) # ['aaa', 2222, 'cccc', 'ddd', 'eee']
print(res) # None (無返回值)
l=["aaa",2222,"cccc"]
res=l.insert(1,"xxx")
print(l) #['aaa', 'xxx', 2222, 'cccc']
print(res) #None (無返回值)
msg=['song','hai','xing','hhh','lihai']
# 通過索引來刪除
del msg[1]
del msg[1:5:2] #del加切片 (hai,hhh)
print(msg)
# 直接通過元素來刪除,沒有返回值
msg.remove('hai')
print(msg)
# 通過索引刪除,並可拿到這個值
msg.pop(1)
msg.pop() #不放置索引默認刪除末尾
print(msg)
# "pop"的特有功能:有返回值
print(msg.pop(1)) #可以彈出這個值
print(msg.remove('hai')) #只刪除,不會返回這個值
l=[111,222,333,444,555,666,777]
for i in l:
print(i)
l=[111,222,333,444,111,555,666,777]
print(l.count(111)) #2
l=[111,222,]
# 字符串
l.extend("hello")
print(l) #[111, 222, 'h', 'e', 'l', 'l', 'o']
# 列表
l.extend(["xxx","yyy"])
print(l) #[111, 222, 'h', 'e', 'l', 'l', 'o', 'xxx', 'yyy']
# 追加字典的話加入的是"key"
msg=['song','hai','xing','song','lihai']
msg.clear()
print(msg) # []
msg=['song','hai','xing','song','lihai']
l=msg.copy()
print(l) # ['song','hai','xing','song','lihai']
msg=['song','hai','xing','hhh','lihai']
print(msg.count('hai')) # 1
msg=['song','hai','xing','song','lihai']
print(msg.index('song',2,4)) # 3 (找不到會報錯)
l=[5,7,9,3,1,4]
l.sort() #排序
print(l)
# 參數"reverse"反序
l.sort(reverse=True) #反向排序
print(l)
li = [1, 'egon', 'alex', 'lxx']
li.reverse()
print(li) # ['lxx', 'alex', 'egon', 1]
# 依照ascll碼表對應的十進制來比較
l1='hello'
l2='k'
print(l1 < l2) #abcd....從小到大來比較
l1=[3,'a','g','j',]
l2=[1,'c','g',]
print(l1 > l2) #要一一對應,不然報錯
print('Z'>'a') #大寫小於小寫A-Z,a-z
print('a'>'G') #大於任何大寫的字母
l=[]
# 入隊列
l.append("first")
l.append("second")
l.append("third")
print(l) #['first', 'second', 'third']
# 出隊
print(l.pop(0)) #first
print(l.pop(0)) #second
print(l.pop(0)) #third
l=[]
# 入棧
l.append("first")
l.append("second")
l.append("third")
print(l) #['first', 'second', 'third']
# 出棧
print(l.pop()) #third
print(l.pop()) #second
print(l.pop()) #first
(後進先出)
l=[]
# 入棧
l.append("first")
l.append("second")
l.append("third")
print(l) #['first', 'second', 'third']
# 出棧
print(l.pop()) #third
print(l.pop()) #second
print(l.pop()) #first