英文文檔:
class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)
Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments.
If no positional argument is given, an empty dictionary is created. If a positional argument is given and it is a mapping object, a dictionary is created with the same key-value pairs as the mapping object. Otherwise, the positional argument must be an iterable object. Each item in the iterable must itself be an iterable with exactly two objects. The first object of each item becomes a key in the new dictionary, and the second object the corresponding value. If a key occurs more than once, the last value for that key becomes the corresponding value in the new dictionary.
If keyword arguments are given, the keyword arguments and their values are added to the dictionary created from the positional argument. If a key being added is already present, the value from the keyword argument replaces the value from the positional argument.
說明:
字典類的構(gòu)造函數(shù)鸟召。
不傳入任何參數(shù)時椭岩,返回空字典蝶溶。
>>> dict()
{}
- 可以傳入鍵值對創(chuàng)建字典衡查。
>>> dict(a = 1)
{'a': 1}
>>> dict(a = 1,b = 2)
{'b': 2, 'a': 1}
- 可以傳入映射函數(shù)創(chuàng)建字典愿卸。
>>> dict(zip(['a','b'],[1,2]))
{'b': 2, 'a': 1}
- 可以傳入可迭代對象創(chuàng)建字典。
>>> dict((('a',1),('b',2)))
{'b': 2, 'a': 1}