• 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()
    {}

       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}

    作者:〖十月狐狸〗

    出处:http://www.cnblogs.com/sesshoumaru/

    欢迎任何形式的转载,但请务必注明出处。

    本人水平有限,如果文章和代码有表述不当之处,还请不吝赐教。

  • 相关阅读:
    C#获取中英文混合字符串长度和截取函数
    页面和块高度在JavaScript中的属性总结
    跨浏览器兼容添加到收藏夹/书签的Javascript
    转:28个免费的在线文件格式转换工具
    2009年十大经典网络小说
    某企业网站建设步骤
    转:Web安全工具大汇聚
    枚举
    VS中不显示解决方案的解决方法
    SQL 分页存储过程
  • 原文地址:https://www.cnblogs.com/sesshoumaru/p/5990468.html
Copyright © 2020-2023  润新知