python3 字典属性
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 字典属性的更多相关文章
- python3 字典常见用法总结
python3 字典常见用法总结 Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 一.创建字典 字典由键和对应值成对组成.字典也被称作关联数组或哈希表 ...
- Python3 字典 get() 方法
Python3 字典 描述 Python 字典 get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...
- Python3 字典 fromkeys()方法
Python3 字典 描述 Python 字典 fromkeys() 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值. 语法 fromkeys()方法语法: ...
- Python3 字典 update() 方法
Python3 字典 描述 Python 字典 update() 函数把字典dict2的键/值对更新到dict里. 语法 update()方法语法: dict.update(dict2) 参数 di ...
- Python3 字典 pop() 方法
Python3 字典 描述 Python 字典 pop() 方法删除字典给定键 key 所对应的值,返回值为被删除的值.key值必须给出. 否则,返回default值. 语法 pop()方法语法: ...
- Python3 字典 clear()方法
Python3 字典 描述 Python 字典 clear() 函数用于删除字典内所有元素. 语法 clear()方法语法: dict.clear() 参数 NA. 返回值 该函数没有任何返回值. ...
- Python3 字典(map)
ayout: post title: Python3 字典(map) author: "luowentaoaa" catalog: true tags: mathjax: true ...
- python系列七:Python3字典dict
#!/usr/bin/python #Python3 字典#字典是支持无限极嵌套的citys={ '北京':{ '朝阳':['国贸','CBD','天阶','我爱我家','链接地产 ...
- python3 字符串属性(一)
python3 字符串属性 >>> a='hello world' >>> dir(a) ['__add__', '__class__', '__contains_ ...
随机推荐
- Linux驱动platform
platform device<==> platform bus <==> platform driver 转自:platform设备驱动全透析 宋宝华 http://blog ...
- 使用 cacti 批量监控服务器以及其 PHP 运作环境配置
http://www.ibm.com/developerworks/cn/linux/l-cn-cacti/ http://www.360doc.com/content/12/0711/22/1465 ...
- ps -ef 和 aux 区别
Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...
- hiho一下 第115周:网络流一•Ford-Fulkerson算法 (Edmond-Karp,Dinic,SAP)
来看一道最大流模板水题,借这道题来学习一下最大流的几个算法. 分别用Edmond-Karp,Dinic ,SAP来实现最大流算法. 从运行结过来看明显SAP+当前弧优化+gap优化速度最快. hi ...
- html5-框架网站
1.html5+:http://www.html5plus.org/ 2.hbuilder:http://www.dcloud.io/ 3.mui:http://dev.dcloud.net.cn/m ...
- 忘记glassfish密码,那就重置密码呗
方法一:如果现有的 domain 上并没有你所需要的东西,删除现有的 domain,重新创建一个 domain. 找到安装glassfish的目录下的 \bin\asadmin 目录,然后打开asad ...
- thrift实例
Thrift实例 Apache thrift是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架. 它采用接口描述语言定义并创建服务,支持可扩展的跨语言服务开发,所包含的代码生 ...
- Spring 定时作业
Spring定时任务的几种实现 近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整理了一下定时任务的几种实现方式,由于项目采用spring框架,所以我 ...
- 【BZOJ4009】[HNOI2015]接水果 DFS序+整体二分+扫描线+树状数组
[BZOJ4009][HNOI2015]接水果 Description 风见幽香非常喜欢玩一个叫做 osu!的游戏,其中她最喜欢玩的模式就是接水果.由于她已经DT FC 了The big black, ...
- swift 一句代码补全tableView分割线
1.swift实现分割线补全 swift一个大进步,只要设置tableView.separatorInset = UIEdgeInsets.zero即可补全分割线, 2.OC实现分割线补全 而在OC中 ...