-
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'}