Python內(nèi)置函數(shù)(15)——dict

英文文檔:

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.

說明:

  1. 字典類的構(gòu)造函數(shù)。

  2. 不傳入任何參數(shù)時,返回空字典。

>>> dict()
{}
  1. 可以傳入鍵值對創(chuàng)建字典。
>>> dict(a = 1)
{'a': 1}
>>> dict(a = 1,b = 2)
{'b': 2, 'a': 1}
  1. 可以傳入映射函數(shù)創(chuàng)建字典。
>>> dict(zip(['a','b'],[1,2]))
{'b': 2, 'a': 1}
  1. 可以傳入可迭代對象創(chuàng)建字典。
>>> dict((('a',1),('b',2)))
{'b': 2, 'a': 1}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 個人筆記,方便自己查閱使用 Py.LangSpec.Contents Refs Built-in Closure ...
    freenik閱讀 67,937評論 0 5
  • Parameters ? A named entity in a function (or method) def...
    龐貝船長閱讀 681評論 0 0
  • 啥叫幸福? 你窮,有人跟著你,這就是幸福; 你病,有人照顧你,這就是幸福; 你冷,有人抱著你,這就是幸福; 你哭,...
    最熱話題精選閱讀 5,958評論 0 50
  • 看過電視劇里有這樣一個橋段。兩個相愛過的人闊別重逢。 男孩說:“你變了,變得更堅強了。” 女孩說:“那我得感謝你,...
    _愛哭鬼閱讀 653評論 0 0
  • 張學(xué)良和妻子的故事,給了我們一個遐想的空間。 一個是情深義重的結(jié)發(fā)妻子。一個是一直陪伴的情人,雖然最終獲得了妻子的...
    科幻經(jīng)典閱讀 472評論 0 0

友情鏈接更多精彩內(nèi)容