兰帕德布劳内

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

创建字典

d = dict(name = "sean",age = 29)

{'age': 29, 'name': 'sean'}

空字典中添加元素

x = {}
x[44] = "foot"
{44: 'foot'}

字典中的copy

x = {"username":"lampard","other":["a","b","c"]}
y = x.copy()

深拷贝

x = {"username":"lampard","other":["a","b","c"]}
y = deepcopy(x)
x["other"].append("sean")

{'username': 'lampard', 'other': ['a', 'b', 'c']}
{'username': 'lampard', 'other': ['a', 'b', 'c', 'sean']}

fromkeys用法

dict.fromkeys(['age','name'])

{'age': None, 'name': None}

get获取键的值

x.get("other")

has_key用法

x.has_key("username")

items和iteritems

x = {"username":"lampard","other":["a","b","c"]}
y = deepcopy(x)
print x.items()
print y.iteritems()

[('username', 'lampard'), ('other', ['a', 'b', 'c'])]
<dictionary-itemiterator object at 0x0000000002CBCF48>

pop删除对应键的值

x.pop("other")
x.popitem()
随机删除一个键值对

update用法

x = {"username":"lampard","other":["a","b","c"]}
y = deepcopy(x)
y["username"] = "sean"
x.update(y)
print x

{'username': 'sean', 'other': ['a', 'b', 'c']}

 

posted on 2016-09-16 23:46  兰帕德布劳内  阅读(74)  评论(0)    收藏  举报