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. [转载]Axure RP 7.0下载地址及安装说明

    Axure RP是产品经理必备的原型制作工具,因为很多同学是新手,在这里整理一下axure7.0的下载.安装和汉化流程,希望能够帮到大家. Axure RP是美国Axure Software Solu ...

  2. apache占用内存高解决办法

    我用512M的vps,访问量不大,但内存占用很大,甚至宕机. 我用top,然后shitf+m发现,httpd占用内存极大.经过网上找资料设置后,用过一段时间终于没再出现内存问题了. 首先查找配置文件的 ...

  3. 刨根问底 HTTP 和 WebSocket 协议(下)

    上篇介绍了HTTP1.1协议的基本内容,这篇文章将继续分析WebSocket协议,然后对这两个进行简单的比较. WebSocket WebSocket协议还很年轻,RFC文档相比HTTP的发布时间也很 ...

  4. JavaWeb学习总结第五篇--认识Cookie机制

    Cookie机制 前言 会话跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie和Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服 ...

  5. 多媒体开发之--- live555 vs2010/vs2013下编译,使用,测试

    Ⅰ live555简介 Live555 是一个为流媒体提供解决方案的跨平台的C++开源项目,它实现了对标准流媒体传输协议如RTP/RTCP.RTSP.SIP等的支持.Live555实现了对多种音视频编 ...

  6. 数据库导入Excel-从基础做起

    近期一直跟着师傅做考试系统的基础.每天与大量的数据打交道.数据的导入.数据的导出.视图的导入导出.核对信息等等,收获挺多的,培养了自己的耐心和细心,也进一步了解了数据库. 一切从基础做起! 来看看近期 ...

  7. 一个手动备份MySQL数据库的脚本

    #!/bin/bash username=root hostname=localhost password=root mysql -u$username -h$hostname -p$password ...

  8. 求最小正整数x,A^x=1(mod M)求阶模板

    整数的阶:设a和n是互素的正整数,使得a^x=1(mod n)成立的最小的正整数x称为a模n的阶 //求阶模板:A^x=1(mod M),调用GetJie(A,M) //输入:10^10>A,M ...

  9. 线段覆盖 2(序列DP)

    Code vs 3027 线段覆盖 2   题目描述 Description 数轴上有n条线段,线段的两端都是整数坐标,坐标范围在0~1000000,每条线段有一个价值,请从n条线段中挑出若干条线段, ...

  10. iOS 开发之RunLoop

    概念 RunLoop 就像她的名字一样,就是跑环,就是一个死循环.是一个可以随时休眠,随时唤醒的死循环. 那么一个手机App为什么会一直运行?而且在接受到用户点击的时候,会做出反应?这些都离不开Run ...