#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) |