使用dict和set
1、dict :是directionAbbreviation for dictionary
1) 通過{ }創建,use health-值(key-value)存儲;用“鍵值對”表示映射關系,例如 {名字:對應的成績},查找速度快,鍵值對之間無序;(Keys are immutable data types,且不能重復;值可以是任意數據類型;)
2)通過’字典名[i]‘to obtain the corresponding value,當i不存在時,會報錯;


3)判斷 key值是否存在:
用 dict.get() 方法判斷:

如果key值不存在,返回None值,Or return the specified onevalue值 

用 in 方法判斷:

4)len(dict) 獲得字典dict中元素的個數

5)dict.keys() 所有鍵; dict.values()所有值; dict.items()所有鍵值對

6) 增加鍵值對: 字典名[鍵]

7)刪除鍵值對:dict.pop(i)

若鍵不存在,The return value can be specifiedvalue

8)通過 “字典名[鍵]”修改值

9)通過for循環遍歷:

10)排序:先把dict.items()轉換為列表,再排序
Sorts the first element by default 

2、set:和dict類似,但不存儲value值
元素不能重復,且必須為不可變數據類型
1) { }Could not create nullset,Because it is used to create an empty dictionary
2) len(A) Dictionary is availableA中元素的個數

2) a.add(s) 增加元素

3) a.remove(s) 刪除元素

4) 集合操作



5)遍歷排序:
for循環遍歷:

Convert the dictionary to a list first,再輸出結果:


2022-08-03