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

set和dict

时间:2019-06-23 17:38:28      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:table   浅拷贝   copy   list   span   min   imp   print   date   

dict属于mapping类型

from collections.abc import Mapping,MutableMapping
from collections.abc import __all__

# dict 属于Mapping类型

a = {}
print(isinstance(a,MutableMapping))

copy 浅拷贝

a = {
    "liming1":{"company":"tencent1"},
    "liming2":{"company":"tencent2"},
}

# clear
""" D.clear() -> None.  Remove all items from D. """
# a.clear()

#copy 浅拷贝
""" D.copy() -> a shallow copy of D """
new_dict = a.copy()
new_dict[liming1][company] = tencent3

print(a)
print(new_dict)

###########
{liming1: {company: tencent3}, liming2: {company: tencent2}}
{liming1: {company: tencent3}, liming2: {company: tencent2}}

fromkeys

# fromkeys
new_list = [liming1,liming2]

new_dict = dict.fromkeys(new_list,{company:tencent})

print(new_dict)

update

# update

new_dict.update({lisa:{"company":"tencent2"}})
new_dict.update({liming2:{"company":"tencent2"}})
new_dict.update(jenny={company:beijing})
new_dict.update([(jenny,{company:shanghai})])
print(new_dict)

 

set和dict

标签:table   浅拷贝   copy   list   span   min   imp   print   date   

原文地址:https://www.cnblogs.com/richiewlq/p/11073272.html

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