Python—字典的操做

字典的操做:spa

#字典的本質實際上是dict類的對象
>>> a = dict([("a","1")])
>>> a
{'a': '1'}

 

一.增長code

1 >>> student_num = {"stu1101": "阿橙", "stu1102": "阿萬", "stu1103": "阿朝", "stu1104": "peter1",}
2 >>> student_num["stu1105"] = "zebra"   #增長
3 >>> print(student_num)
4 {'stu1101': '阿橙', 'stu1102': '阿萬', 'stu1103': '阿朝', 'stu1104': 'peter1', 'stu1105': 'zebra'}
5 >>>

 

二.修改對象

1 >>> student_num = {"stu1101": "阿橙", "stu1102": "阿萬", "stu1103": "阿朝", "stu1104": "peter1",}
2 >>> student_num["stu1101"] = "gkx"  #修改
3 >>> student_num
4 {'stu1101': 'gkx', 'stu1102': '阿萬', 'stu1103': '阿朝', 'stu1104': 'peter1', 'stu1105': 'zebra'}

 

三.刪除blog

 1 >>> #刪除
 2 >>> student_num.pop("stu1104")  #標準刪除方法  3 'peter1'
 4 >>> student_num
 5 {'stu1101': 'gkx', 'stu1102': '阿萬', 'stu1103': '阿朝', 'stu1105': 'zebra'}
 6 >>> 
 7 >>> 
 8 >>> del student_num["stu1105"]  9 >>> student_num
10 {'stu1101': 'gkx', 'stu1102': '阿萬', 'stu1103': '阿朝'}
11 >>> #ptyhon內置的刪除方法
12 >>> 
13 >>> student_num.popitem() #隨機刪除 14 ('stu1103', '阿朝')
15 >>> student_num
16 {'stu1101': 'gkx', 'stu1102': '阿萬'}
17 >>> 

 

四.查找字符串

 1 >>> #查找
 2 >>> zoo = {"a101":"zebra","b202":"chimpanzee","c303":"lion"}
 3 >>> "a101" in zoo #判斷字典中key是否在dict中
 4 True
 5 >>> 
 6 >>> zoo["a101"]  7 'zebra'
 8 >>> #用key來查找value,不過當key不在該dict中時,會報錯
 9 >>> 
10 >>> zoo.get("d404") 11 >>> print(zoo.get("d404"))
12 None
13 >>> print(zoo.get("c303"))
14 lion
15 >>> #用.get的方法,找不到不會報錯,建議使用

 

五.字典的循環和自動生成get

 
 

  >>> zoo.values()  #打印全部value
  dict_values(['zebra', 'chimpanzee', 'lion'])
  >>> zoo.keys()    #打印全部key
  dict_keys(['a101', 'b202', 'c303'])it

 1 >>> zoo2
 2 {'c303': 'lion', 'd404': 'elephant', 'c4': 'tiger'}
 3 >>> for key in zoo2: #打印key和value
 4     print(key,zoo2[key])
 5 
 6 >>>
 7 c303 lion
 8 d404 elephant
 9 c4 tiger
10 >>> 
11 
12 >>> for k,v in zoo2.items(): #數據量大時不要用
13     print(k,v)
14 
15 >>>    
16 c303 lion
17 d404 elephant
18 c4 tiger
19 >>> 
20 
21 >>> c = dict.fromkeys([1,2,3],[4,5,6]) #位置1是key,位置2無論寫啥都是values,不過若位置2爲[4:{"name":"6"}],生成的字典,若修改其中一個[4:{"name":"6"}]字典裏的值,其餘value也都會一塊兒改,
相似淺copy
22 print(c) 23 {1: [4,5,6], 2: [4,5,6], 3: [4,5,6]}

 

六.字典的其餘操做io

 1 >>> #setdefault
 2 >>> zoo2 = {"a101":"zebra","b202":"chimpanzee","c303":"lion"}
 3 >>> zoo2.setdefault("a101","cat") #setdefault 判斷key是否在dict中,若在則不作修改,若不在則將新key添加至dict中  4 'zebra'
 5 >>> zoo2
 6 {'a101': 'zebra', 'b202': 'chimpanzee', 'c303': 'lion'}
 7 >>> zoo2.setdefault("d404","elephant")  8 'elephant'
 9 >>> zoo2
10 {'a101': 'zebra', 'b202': 'chimpanzee', 'c303': 'lion', 'd404': 'elephant'}
11 >>> 
12 
13 >>> zoo2
14 {'c303': 'lion', 'd404': 'elephant'}
15 >>> dict_b = {1:2,2:3,"c4":"tiger"}
16 >>> zoo2.update(dict_b) #相似列表中的extend,若 dict_b中有和zoo2交叉的key,會更新爲dict_b中的valu 17 >>> zoo2
18 {'c303': 'lion', 'd404': 'elephant', 1: 2, 2: 3, 'c4': 'tiger'}
19 
20 >>> #items
21 >>> zoo2.items() #將字典中的key、value值對,一對對拿出,生成爲新列表 22 dict_items([('c303', 'lion'), ('d404', 'elephant'), (1, 2), (2, 3), ('c4', 'tiger')])
23 >>> 

 字典的方法有:fromkeys , pop , popitem , setdefault , clear , copy , keys , values , items , update , getclass

字典只能存字符串或者二進制:date

若是想存其餘格式的,要用str() 強制轉換一下

相關文章
相關標籤/搜索