天天看點

Python dict() 函數

描述

dict() 函數用于建立一個字典。 

文法

dict 文法:

class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)      
  • **kwargs -- 關鍵字
  • mapping -- 元素的容器。
  • iterable -- 可疊代對象。

傳回值

執行個體

>>>dict() # 建立空字典 {} >>> dict(a='a', b='b', t='t') # 傳入關鍵字 {'a': 'a', 'b': 'b', 't': 't'} >>> dict(zip(['one', 'two', 'three'], [1, 2, 3])) # 映射函數方式來構造字典 {'three': 3, 'two': 2, 'one': 1} >>> dict([('one', 1), ('two', 2), ('three', 3)]) # 可疊代對象方式來構造字典 {'three': 3, 'two': 2, 'one': 1} >>>