Python_API_Built-in Types_dict.update

来源:互联网 发布:金蝶k3软件下载 编辑:程序博客网 时间:2024/06/04 17:55

update([other])

Update the dictionary with the key/value pairs fromother, overwriting existing keys. ReturnNone.

update() accepts either another dictionary object or an iterable of key/value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key/value pairs:d.update(red=1,blue=2).

Changed in version 2.4:Allowed the argument to be an iterable of key/value pairs and allowed keyword arguments.

update([other])
将参数other映射到dictionary中,返回None
             该方法接受另一个字典对象一个键/值对迭代长度为二元组其他iterables如果指定关键字参数然后用这些键/值对更新字典d.update红色,蓝色= 1= 2)    

例:#! /usr/bin/env python
#coding=utf-8

dict={'name':'lyle','id':'l'}
print type(dict)
print dict
dict.update(name=1)
print dict
a = (('a','b'),('1','2'))#注意这里必须是2个以上
dict.update(a)
print a
dict.update({'title':'title1','id':'2'})

print dict


输出:<type 'dict'>
{'name': 'lyle', 'id': 'l'}
{'name': 1, 'id': 'l'}
(('a', 'b'), ('1', '2'))
{'a': 'b', '1': '2', 'name': 1, 'title': 'title1', 'id': '2'}




原创粉丝点击