天天看點

python dict函數

dict()函數用于建立一個字典,傳回值為一個字典

dict()文法

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