码迷,mamicode.com
首页 > 其他好文 > 详细

dict

时间:2020-02-02 23:35:43      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:clear   成员   ISE   add   deepcopy   one   深拷贝   bsp   use   

字典常用的方法包含:

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

 

10 dict的拷贝
my_dict = {"bobby": 22, "bobby1": 33, "imooc": {"hello":11}}
a = my_dict.copy()
print(a)
a["imooc"]["hello"]=45
#浅拷贝-------没有做嵌套的拷贝
import copy
new_dict = {"bobby": 22, "bobby1": 33, "imooc": {"hello":11}}
b = copy.deepcopy(new_dict)
b["imooc"]["hello"] = 99
pass
#深拷贝-----完全将数据做了一个嵌套的拷贝

 

 

 

 

例如:
#返回字典的成员个数;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)

dict

标签:clear   成员   ISE   add   deepcopy   one   深拷贝   bsp   use   

原文地址:https://www.cnblogs.com/wenshu/p/12253013.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!