1. 程式人生 > >Python資料型別之dict相關常用操作

Python資料型別之dict相關常用操作

info = {

    'stu1101': "TengLan Wu",

    'stu1102': "LongZe Luola",

    'stu1103': "XiaoZe Maliya",

}

 

#取值

print(info["stu1101"])    #取出按鍵的值

 

#修改

info["stu1101"] = "刀前衛"  #修改key的值

info["stu1104"] = "changjinkong" #修key的值 如果沒有key就自動新增key

 

#刪除

#del info["stu1101"] #刪除改key(值自然就沒了)

info.pop("stu1101") #刪除該key 和上面效果一樣

print(info)

 

#查詢

#info['stu1104'] #用該方式查詢,如果沒有該key就會報錯,所以不用

print(info.get('stu1103')) #用.get('xxx')查詢是最有用的

print(info.get('stu1105'))  #如果沒有該key直接回復none而不是報錯

 

#判斷key會否存在

print('stu1103'in info) #判斷是否有該key #python2:info.has_key("1103") 效果一樣,但是3消失了

 

#判斷key會否存在,存在就不進行替換,不存在就新增

info. setdefault('stu1103', "XiaoZe Maliya")

 

#字典合併

info = {

    'stu1101': "TengLan Wu",

    'stu1102': "LongZe Luola",

    'stu1103': "XiaoZe Maliya",

}

 

b ={

    'stu1101':"Burgess",

    1:3,

    2:5

}

 

info.update(b)  #把b該字典更新到info字典內(合併起來) 相同key 那麼會吧原先的字典值覆

 

#使用.fromkeys建立新字典(一旦字典的value一修改,所有的都會修改)

c = dict.fromkeys([6,7,8],"test") 

#結果c=={8: 'test', 6: 'test', 7: 'test'}

d = dict.fromkeys([6,7,8],[1,{"name":"burgess"},444]) 

#結果d=={8: [1, {'name': 'burgess'}, 444], 6: [1, {'name': 'burgess'}, 444], 7: [1, {'name': 'burgess'}, 444]}

d[8][1]['name'] = "Jack "

#結果d=={8: [1, {'name': 'Jack '}, 444], 6: [1, {'name': 'Jack'}, 444], 7: [1, {'name': 'Jack'}, 444]}

 

 

#Python字典列表取值

__author__ = "Burgess Zheng"

USER_DICT = {

    '1':{'name':'root1','email':'[email protected]'},

    '2':{'name':'root2','email':'[email protected]'},

    '3':{'name':'root3','email':'[email protected]'},

    '4':{'name':'root4','email':'[email protected]'},

    '5':{'name':'root5','email':'[email protected]'},

}

for i in USER_DICT.keys():  #迴圈取該字典的key

    print(i)  #列印結果是 1 2.....

for i in USER_DICT.values():  #迴圈取該字典的value

    print(i)  #列印結果是([{'name':'root1','email':'[email protected]'},{‘name’:root2....}]

for k,i in USER_DICT.items():#獲取key value

    print(i)  #列印value  結果{'email': '[email protected]', 'name': 'root2'}

    print(k)  #列印key   結果2

 

print(USER_DICT["1"])#列印key=1value

                    # 列印結果 {'name': 'root1', 'email': '[email protected]'}

print(USER_DICT["1"]["email"])#列印key=1valuekey=emailvalue

                    # 列印結果 [email protected]

print(list(USER_DICT.values()))

                   # 列印結果 [{'name': 'root1', 'email': '[email protected]'},{‘name’:root2....},....]

print(list(USER_DICT.key()))

                   # 列印結果 [‘1’,’2’....’5’]

print(list(a.items()))

                   # 列印結果 [(‘1’,{‘name’:root1....),(‘2’,{‘name’:root2...}),()...]

 

USER_LIST = [

    {'name':'root1','email':'[email protected]'},

    {'name':'root2','email':'[email protected]'},

    {'name':'root3','email':'[email protected]'},

    {'name':'root4','email':'[email protected]'},

    {'name':'root5','email':'[email protected]'},

]

for i in USER_LIST:#迴圈取該列表所有的元素

    print(i)#列印結果是{'name': 'root1', 'email': '[email protected]'}

 

print(USER_LIST[0])  #列印該列表下標0value

                    #結果:{'email': '[email protected]', 'name': 'root1'}

print(USER_LIST[0]["name"])#列印該列表下標0valuekey=name的值

                                                        #結果:root1

 

#sorted 升序

a = {6:2,8:0,1:4,-5:6,99:11,4:22}

print(a)  #我們知道字典是無序的

print(sorted(a))   #預設key升序,變成列表 value消失

print(sorted(a.items()))  #預設key升序,變成列表 value存在

print(sorted(a.items(),key=lambda x:x[1]))  #按value升序 變成列表

#key=lambda x=key:x[1]取該key的value  等於字典的key作為傳參

#要排序就必須變成列表 items就是變成列表