python dict常用操作


假設:目前已在字典dict_stu,它的定義如下:

dict_stu = {
"171001":{
"name":"xiaohua",
"age":21,
"sex":'g',
},
"171002":{
"name":"xiaoli",
"age":21,
"sex":'g',
},
}

字典常用的方法包含:

  1、增加key-value;通過dict_stu[key_new]={value_new};  通過dict_stu.update(dict_new);  

  2、修改某個key對應的value;通過dict_stu[key_modify]={values_new}

  3、查找某個key對應的value;通過dict_stu[key_find];  通過dict_stu.get(key_find);  通過dict_stu.setdefault(key_find,"defualt value"); 

  3.1、返回字典中所有的值;通過dict_stu.values()

  4、刪除某個key對應的value;通過del dict_stu[key_del]; 通過dict_stu.pop(key_del);  

  5、復制一個字典:通過dict_stu.copy()

  6、判斷字典key是否在字典中:通過key in dict_stu;  通過key not in dict_stu

  7、計算字典的長度:通過len(dict_stu)

  8、創建一個(不)含默認值的字典:dict.fromkeys(key_list,values)

  9、刪除整個字典:通過del dict_stu;  通過dict_stu.clear()

測試示例:

#author FK
dict_stu = {
"171001":{
"name":"xiaohua",
"age":21,
"sex":'g',
},
"171002":{
"name":"xiaoli",
"age":21,
"sex":'g',
},
}


new_stu = {
"171008":{
"name":"lili",
"age":23,
"sex":'g',
},
"171002":{
"name":"lulu",
"age":20,
"sex":'g',
}

}

#增加或修改字典中的數據,如果沒有對應的key便增加,如果有對應的key便修改;dict insert data
dict_stu["171003"]={
"name":"xiaoliang",
"age":22,
"sex":'m',
}

#返回字典的成員個數;return the number of items in the dictionary
print("after add item.the length of dict is:",len(dict_stu))

#刪除字典某個key的成員,如果沒有key拋出異常;remove dict_stu[key] from dict,Raises a KeyError if key is not in the map
del dict_stu["171003"]

#返回並刪除字典中某個key的值或default,如果key存在返回key對應的值,如果key不存在,設置了default則返回defalut值,如果沒設置則報錯
print("\033[31;1mtest the pop method:\033[0m",dict_stu.pop("171002"))
#pop方法沒有設置default,測試報錯
#print("\033[31;1mtest the pop method,there is no 171002 key\033[0m",dict_stu.pop("171002"))
#pop方法有設置default,測試正常
print("\033[31;1mtest the pop method,there is no 171002 key:\033[0m",dict_stu.pop("171002","the default is defined"))

#return the number of items in the dictionary
print("after del item.the length of dict is:",len(dict_stu))

#測試key是否在字典中,如果存在返回true ,同key not in d;retrun True if dict_stu has a key 'key' ,else False
print("\033[31;1mtest the 171003 is the dict_stu's key?\033[0m","171003" in dict_stu)

#返回字典中key對應的value,如何沒有返回None;retrun the value for key if key is in the dictionary,else default return None
print("return the 171001's values:",dict_stu.get("171001"))

#如果key在字典中,返回字典中key對應的value;如果key沒有在字典中,返回默認值
print("\033[31;1msetdefault methord return the 171001's values:\033[0m",dict_stu.setdefault("171001","default values"))
print("\033[41;1msetdefault methord return the 171008's values:\033[0m",dict_stu.setdefault("171008","default values"))

#方法1:升級一個字典;update the dictionary
dict_stu.update(new_stu)
print("\033[21;1muse update method update the dict_stu:\033[0m",dict_stu)

#字典的淺復制
dict_copy = dict_stu.copy();
print("\033[41;1mthe copy of the dict_stu\033[0m",dict_copy)
print("test the copy dict is equle the org dict:",dict_copy == dict_stu)
print("the dict of dict_stu",dict_stu)

#return a new view of the dictionary's values
print("\nthe dict_stu.values methord:\t",dict_stu.values())

#創建一個字典通過fromkeys方法,以序列seq中元素做字典的鍵,value為字典所有鍵對應的初始值
dict1_fromkeys = dict.fromkeys([1,2,3,4],50)
dict2_fromkeys = dict.fromkeys([1,2,3,4],[50,20,10,79])
dict3_fromkeys = dict.fromkeys([1,2,3,4],set([50,20,10,79]))
print("\033[31;1mcreate a dict with keys from seq and values set\033[0m",dict1_fromkeys)
print("\033[31;1mcreate a dict with keys from seq and values set\033[0m",dict2_fromkeys)
print("\033[31;1mcreate a dict with keys from seq and values set\033[0m",dict3_fromkeys)
dict1_fromkeys[1]="修改"
#由於是淺復制,所以修改列表中的一個元組,其它節點就都修改了
dict2_fromkeys[1][0]="90"
print("\033[31;1mmodify the dict with keys from seq and values set\033[0m",dict1_fromkeys)
print("\033[31;1mmodify the dict with keys from seq and values set\033[0m",dict2_fromkeys)

#返回字典中所有的值
print("return a new view of the dictionary's values:",dict_stu.values())
#remove all items from the dictionary
dict_stu.clear()
print("after clear the dict is:",dict_stu)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM