假设:目前已在字典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 FKdict_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 datadict_stu["171003"]={ "name":"xiaoliang", "age":22, "sex":'m',}#返回字典的成员个数;return the number of items in the dictionaryprint("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 mapdel 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 dictionaryprint("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 Falseprint("\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 Noneprint("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 dictionarydict_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 valuesprint("\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 dictionarydict_stu.clear()print("after clear the dict is:",dict_stu) |
来源:https://www.cnblogs.com/fkblogmx/p/7825805.html