清單
A=['cal','what',1,'2020-1-1','join']
**
增加元素
**
1.append
A.append("Hello") #元素
2.extend
A.extend([4,'much',1]) #清單
3.insert
A.insert(3,"hello") #元素,固定位置添加(通過索引)
**
删除元素
**
1.pop
B=A.pop() #索引移除,預設最後一個
2.remove
A.remove("what") #通過元素名稱,移除
3.del
del A[2] #索引移除
**
隊列翻轉
**
1、reverse
A.reverse() #清單翻轉 例如:
# [3,1,'what']變為['what',1,3]
**
元素計數
**
1、count
C=['Hello','Google','Hello','baidu',
'Hello','baidu','mofa','guiyi']
print(count('Hello'))
結果: 3
**
隊列排序
**
1、sort
D=[2,8,44,5,12,1]
D.sort()
元組
定義元組
ll=(1,2,3)
print(ll)
print(ll.type())
結果 : (1,2,3)
<class 'tuple'>
注:如果元組中隻有一個元素,則這個元組後面必須要有一個",",否則元素就還是原來的類型
例如 :
A=(1,)
正确
是元組
A=(1)
****
錯誤
不是元組
**
删除元素
**
由于元組不能修改,是以元組也不能删除部分元素,要删除隻能删除整個元組
元素計數
1、count
C=('Hello','Google','Hello','baidu',
'Hello','baidu','mofa','guiyi')
print(count('Hello'))
結果: 3
**
查找元素索引
**
A=('cal','what',1,'2020-1-1','join')
print("what index is:",A.index("what"))
結果是 : what index is:1
**
字典
**
字典定義:字典類型就和他的名字一樣,可以向查字典一樣去找,其他語言也有類似的類型。例如:Java中的HashMap , PHP中的Array等
chinese={"we":"我們",
"are from":"來自",
"China":"中國"
}
print(chinese)
print("China:",chinese["China"]) #鍵查找
結果: {'we': '我們', 'are from': '來自', 'China': '中國'}
China:中國
注意:字典的鍵必須是唯一的,不重複的,如果是空字典,直接用{}表示
empty={}
print(type(empty))
結果 : <class 'dict'>
**
修改鍵值
**
chinese={"we":"我們",
"are from":"來自",
"China":"中國"
}
print(chinese)
chinese["China"]="未來"
print(chinese)
結果:{'we': '我們', 'are from': '來自', 'China': '中國'}
{'we': '我們', 'are from': '來自', 'China': '未來'}

**
删除字典元素
**
chinese={"we":"我們",
"are from":"來自",
"China":"中國"
}
print(chinese)
del chinese["China"]
print(chinese)
**
清空元素
**
chinese={"we":"我們",
"are from":"來自",
"China":"中國"
}
print(chinese)
# chinese["China"]="未來"
# del chinese["China"]
chinese.clear()
print(chinese)
**
複制字典
**
用于修改複制的字典,相當于複制一個新的字典作為修改,原有的字典不變
chinese={"we":"我們",
"are from":"來自",
"China":"中國"
}
Chi=chinese.copy()
**
建立新字典
**
SQ={"name","age","sex"}
student=dict.fromkeys(SQ)
print(student)
student1=dict.fromkeys(SQ,15)
print(student1)
傳回鍵對值
chinese={“we”:“我們”,
“are from”:“來自”,
“China”:“中國”
}
print(chinese.get(“we”))
**
傳回一個清單,包含字典所有鍵
**
chinese={"we":"我們",
"are from":"來自",
"China":"中國"
}
print(chinese.keys())
**
傳回一個清單,包含字典所有值
**
chinese={"we":"我們",
"are from":"來自",
"China":"中國"
}
print(chinese.values())
**
傳回一個清單,包含字典所有鍵和值
**
chinese={"we":"我們",
"are from":"來自",
"China":"中國"
}
print(chinese.items())
**
周遊字典
**
注意:因為字典不能直接應用于for循環,我們可以使用items方法來周遊字典
chinese={"we":"我們",
"are from":"來自",
"China":"中國"
}
print(chinese.items())
for k,v in chinese.items():
print(k,"=>",v)
**
集合
**