前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python dict()方法学习笔记

python dict()方法学习笔记

作者头像
py3study
发布2020-01-09 16:31:23
6270
发布2020-01-09 16:31:23
举报
文章被收录于专栏:python3python3

学习PYTHON 的dict()方法笔记。

  dict() -> new empty dictionary

 |  dict(mapping) -> new dictionary initialized from a mapping object's

 |      (key, value) pairs

 |  dict(iterable) -> new dictionary initialized as if via:

 |      d = {}

 |      for k, v in iterable:

 |          d[k] = v

 |  dict(**kwargs) -> new dictionary initialized with the name=value pairs

 |      in the keyword argument list.  For example:  dict(one=1, two=2)

dict()方法四种样式:

dict():   原型

dict(maping):    映射 

dict(iterable):   迭代

dict(**kwargs):  解包,函数调用

原型:

dict():用于建立空的字典

映射: 迭代样式:

在我看来,没什么区别(个人看法,欢迎交流)

iterable/mapping  -->  只能是一种可迭代数据结构,且只有是一个,不可能为多个,可以是一个元组、列表,其元素格式为(key, value)或是[key, value](要以这种样式,是最重要的,也是我认为map,iterable,这两种方式是一样的)。

如:dict('a','b')  会出现错误:

TypeError: dict expected at most 1 arguments, got 2(出现多个参数)

如:dict((['a', 'b'],('c', 'd'))),元组的元素可是列表,也可是元组,但要以(key, value)样式出现。

解包,函数调用样式:

1、**kwargs 可以字典解包,而kwargs是一个字典;

d1 = {'a': 'b', 'c': 'd'}

d2 = dict(**d1)

d2 -->   {'a': 'b', 'c': 'd'}

2、函数调用:将dict()做为一个函数,通过关键字方式来进行调用,类似这样的方式。

dict(a=1, b=2)  -->   {'a':1, 'b':2}  可以得到一个以参数名为key, 参数值为value的字典。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档