Python內置函數(15)——dict

英文文檔:app

class dict(**kwarg) 函數

class dict(mapping, **kwarg) spa

class dict(iterable, **kwarg) code

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.blog

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.ip

 

說明:文檔

  1. 字典類的構造函數。it

  2. 不傳入任何參數時,返回空字典。io

>>> dict()
{}

   3. 能夠傳入鍵值對建立字典。

>>> dict(a = 1)
{'a': 1}
>>> dict(a = 1,b = 2)
{'b': 2, 'a': 1}

   4. 能夠傳入映射函數建立字典。

>>> dict(zip(['a','b'],[1,2]))
{'b': 2, 'a': 1}

   5. 能夠傳入可迭代對象建立字典。

>>> dict((('a',1),('b',2)))
{'b': 2, 'a': 1}
相關文章
相關標籤/搜索