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. 内核initcall分析

    linux中init相关内容定义在include/linux/init.h initcall相关定义 先看下文件说明,此文件定义的宏主要用于初始化阶段标记函数或初始化数据,之后占用的资源会被释放掉. ...

  2. Oracle PL/SQL 高级编程

    1. 复合数据类型--记录类型 Ø        语法格式 type  类型名 is   record ( 字段1  字段1类型 [not null]:=表达式1; 字段2  字段2类型 [not n ...

  3. linux下网卡bonding配置

    linux下网卡bonding配置   章节 bonding技术 centos7配置bonding centos6配置bonding 一.bonding技术 bonding(绑定)是一种linux系统 ...

  4. java实体类如果不重写toString方法,会如何?

    先认识一下Object Object 类的 toString 方法 返回一个字符串,该字符串由类名(对象是该类的一个实例).at 标记符“@”和此对象哈希码的无符号十六进制表示组成.换句话说,该方法返 ...

  5. easyui combobox 三级级联 input 两种实现

    /**<img src='${pageContext.request.contextPath}/images/block.png'/> * @param 默认载入 省市 */ $(func ...

  6. Linux mail发送邮件

    发送前一天的监控日志#!/bin/bash source /etc/profile time=`date -d '-1 day' "+%Y%m%d"` #判断服务是否已经启动 se ...

  7. 趣味编程:C#中Specification模式的实现(参考答案 - 下)

    一篇文章中我们利用C#语言的特性实现了一种轻量级的Specification模式,它的关键在于抛弃了具体的Specification类型,而是使用一个委托对象代替唯一关键的IsSatisfiedBy方 ...

  8. java 十进制转十六进制、十进制转二进制、二进制转十进制、二进制转十六进制

    //10进制转16进制 Integer.toHexString(20); //10进制转2进制 Integer.toBinaryString(10); //16进制转10进制 Integer.pars ...

  9. XML使用总结(一)

    XML使用总结(一): XML是一种可拓展的标记语言,被设计用来描写叙述.存储及传递数据的语言体,而它的标签没有被提前定义,须要用户自行定义,是W3C推荐的数据存储和传递的标准标记语言. ·      ...

  10. Android 热门技术干货

    http://mp.weixin.qq.com/s?__biz=MzIwMzYwMTk1NA==&mid=2247484939&idx=1&sn=d1871b09de55ca6 ...