1、字典创建

 >>> D={}
>>> D
{} >>> D2={:'a','key':,(,):'e','d':{:'w',:'d'}} #冒号构造 1、使用 {  }和 : 直接创建
>>> D2
{: 'a', 'key': , 'd': {: 'w', : 'd'}, (, ): 'e'} >>> D3=dict([('a',),('b',)]) #使用可以是一对数据组成的元组或列表,可以使用zip 2、使用dict和成对数据创建
>>> D3
{'b': , 'a': } >>> D4=dict(name='bob',age=) #使用等号构造 3、使用dict和 键=值 创建
>>> D4
{'age': , 'name': 'bob'}
>>>

zip函数创建

 >>> name=('tom','bob','harry')
>>> age=(,,)
>>> D5=dict(zip(name,age)) 4、使用dict和zip(两个序列)创建
>>> D5
{'tom': , 'bob': , 'harry': }

dict.formkeys创建字典

fromkeys(iterable, value=None, /) method of builtins.type instance
Returns a new dict with keys from iterable and values equal to value.

 >>> a=('tom','jerry','bob')
>>> D6=dict.fromkeys(a,'student') 5、使用dict.fromkeys(序列,默认值)创建
>>> D6
{'tom': 'student', 'jerry': 'student', 'bob': 'student'}

字典解析

 >>> D7={'%s'%x:x** for x in range() if x%==}
>>> D7
{'': , '': , '': , '': , '': }

2、字典属性

 >>> dir(dict)
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

3、访问字典

 >>> D={'a':,'b':,'c':}
>>> D['a']

属性访问

 >>> D.keys()
dict_keys(['b', 'a', 'c']) #键
>>> D.values()
dict_values([, , ]) #值
>>> D.items()
dict_items([('b', ), ('a', ), ('c', )]) #键+值

D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.

 >>> D
{'b': , 'a': , 'c': }
>>> D.get('a') >>> D.get('d')
>>> D.get('d','Not Found')
'Not Found'

D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D 如果没有找到k,则设置D[k]=d    测试键是否存在,如果不存在则增加该键,值可以设置,默认为None。

 >>> D
{'d': , 'b': , 'a': , 'e': None, 'c': }
>>> D.setdefault('a') >>> D.setdefault('e')
>>> D
{'d': , 'b': , 'a': , 'e': None, 'c': }
>>> D.setdefault('e','m')
>>> D
{'d': , 'b': , 'a': , 'e': None, 'c': }

4、删除字典

删除所有

 >>> D={'a':,'b':}
>>> D.clear()
>>> D
{}

根据键删除单个条目

 >>> D
{'b': , 'a': , 'c': }
>>> del D['a']
>>> D
{'b': , 'c': }

D.pop

D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised

 >>> D.pop('a',)

 >>> D.pop('a')   #要删除的键没有找到且没有设置返回值
Traceback (most recent call last):
File "<stdin>", line , in <module>
KeyError: 'a'
>>> D.pop('b')

D.popitem() -> (k, v), remove and return some (key, value) pair as a
2-tuple; but raise KeyError if D is empty.

 >>> D={'a':,'b':,'c':}
>>> D.popitem()
('b', )
>>> D
{'a': , 'c': }
>>> D.popitem()
('a', )
>>> D
{'c': }
>>> D.popitem()
('c', )
>>> D
{}
>>> D.popitem()
Traceback (most recent call last):
File "<stdin>", line , in <module>
KeyError: 'popitem(): dictionary is empty'

5、增加、修改条目

 >>> D={'a':,'b':,'c':}
>>> D['a']=
>>> D
{'b': , 'a': , 'c': }
>>> D['d']=
>>> D
{'d': , 'b': , 'a': , 'c': }

合并字典

 >>> D={'a':,'b':}
>>> D2={'test':}
>>> D.update(D2)
>>> D
{'b': , 'a': , 'test': }

python3 字典属性的更多相关文章

  1. python3 字典常见用法总结

    python3 字典常见用法总结 Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 一.创建字典 字典由键和对应值成对组成.字典也被称作关联数组或哈希表 ...

  2. Python3 字典 get() 方法

     Python3 字典 描述 Python 字典 get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...

  3. Python3 字典 fromkeys()方法

     Python3 字典 描述 Python 字典 fromkeys() 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值. 语法 fromkeys()方法语法: ...

  4. Python3 字典 update() 方法

     Python3 字典 描述 Python 字典 update() 函数把字典dict2的键/值对更新到dict里. 语法 update()方法语法: dict.update(dict2) 参数 di ...

  5. Python3 字典 pop() 方法

     Python3 字典 描述 Python 字典 pop() 方法删除字典给定键 key 所对应的值,返回值为被删除的值.key值必须给出. 否则,返回default值. 语法 pop()方法语法: ...

  6. Python3 字典 clear()方法

     Python3 字典 描述 Python 字典 clear() 函数用于删除字典内所有元素. 语法 clear()方法语法: dict.clear() 参数 NA. 返回值 该函数没有任何返回值. ...

  7. Python3 字典(map)

    ayout: post title: Python3 字典(map) author: "luowentaoaa" catalog: true tags: mathjax: true ...

  8. python系列七:Python3字典dict

    #!/usr/bin/python #Python3 字典#字典是支持无限极嵌套的citys={    '北京':{        '朝阳':['国贸','CBD','天阶','我爱我家','链接地产 ...

  9. python3 字符串属性(一)

    python3 字符串属性 >>> a='hello world' >>> dir(a) ['__add__', '__class__', '__contains_ ...

随机推荐

  1. Weka学习之预处理连接MySql(二)

    载入数据 (一)打开文件 (二) 打开url (三) 打开数据库 (四)从一些数据生成器(DataGenerators)中生成人造数据    这篇主要写(三)中的连接mySql          网上 ...

  2. Problem A. Dynamic Grid

    Problem We have a grid with R rows and C columns in which every entry is either 0 or 1. We are going ...

  3. POJ 1654 area 解题

    Description You are going to compute the area of a special kind of polygon. One vertex of the polygo ...

  4. 【JMeter4.0学习(三)】之SoapUI创建WebService接口模拟服务端以及JMeter对SOAP协议性能测试脚本开发

    目录: 创建WebService接口模拟服务端 下载SoapUI 新建MathUtil.wsdl文件 创建一个SOAP项目 接口模拟服务端配置以及启动 JMeter对SOAP协议性能测试脚本开发 [阐 ...

  5. 一、任天堂ns (Nintendo Switch) 上手

    公司不方便回家详解做个博客非专业评测~

  6. JSP HTTP 状态码

    JSP HTTP 状态码 HTTP请求与HTTP响应的格式相近,都有着如下结构: 以状态行+CRLF(回车换行)开始 零行或多行头模块+CRLF 一个空行,比如CRLF 可选的消息体比如文件,查询数据 ...

  7. cocos2d-x 2.x 支持多个方向屏幕翻转

    主要改动 RootViewController.mm 的 supportedInterfaceOrientations 方法 1.四个方向 UIInterfaceOrientationMaskAll ...

  8. 解决ubuntukylin下各种终端字母重叠的方案

    ubuntukylin14.04什么都挺好定符合中国人的使用习惯的,可是就是终端字母重叠的问题特别严重;(事实上ubuntu14.04也存在这个问题) 导致非常多非常好用的终端都使用不了,像guake ...

  9. TensorFlow CNN 測试CIFAR-10数据集

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50738311 1 CIFAR-10 数 ...

  10. 利用freemarker生成带fusioncharts图片的word简报

    /**  * 利用freemarker生成带fusioncharts图片的word简报  *         烟台海颐软件技术论坛  *         作者  牟云飞 新建 *         毕业 ...