Python内置函数(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. 字典类的构造函数。

  2. 不传入任何参数时,返回空字典。

>>> dict()
{}
  1. 可以传入键值对创建字典。
>>> dict(a = 1)
{'a': 1}
>>> dict(a = 1,b = 2)
{'b': 2, 'a': 1}
  1. 可以传入映射函数创建字典。
>>> dict(zip(['a','b'],[1,2]))
{'b': 2, 'a': 1}
  1. 可以传入可迭代对象创建字典。
>>> dict((('a',1),('b',2)))
{'b': 2, 'a': 1}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 个人笔记,方便自己查阅使用 Py.LangSpec.Contents Refs Built-in Closure ...
    freenik阅读 67,879评论 0 5
  • Parameters ¶ A named entity in a function (or method) def...
    庞贝船长阅读 3,821评论 0 0
  • 啥叫幸福? 你穷,有人跟着你,这就是幸福; 你病,有人照顾你,这就是幸福; 你冷,有人抱着你,这就是幸福; 你哭,...
    最热话题精选阅读 11,128评论 0 50
  • 看过电视剧里有这样一个桥段。两个相爱过的人阔别重逢。 男孩说:“你变了,变得更坚强了。” 女孩说:“那我得感谢你,...
    _爱哭鬼阅读 3,746评论 0 0
  • 张学良和妻子的故事,给了我们一个遐想的空间。 一个是情深义重的结发妻子。一个是一直陪伴的情人,虽然最终获得了妻子的...
    科幻经典阅读 3,199评论 0 0

友情链接更多精彩内容