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_ ...
随机推荐
- NPOI 计算单元格高度
需求 要导出一个Excel,第一行是不定长字符串,合并单元格(A-G)已知,现要计算第一行的高度. 思路 已知,NPOI的宽度单位是1/256个英文字符,在本例中,A列的宽度是2048,即 2048 ...
- leetCode(37):Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- java面试的那些事
跳槽面临的第一个难关那就是面试吧.面试的好坏直接关乎着你年薪的多少.如何顺利完成面试的那些难题,今天我们就从java中复习一下.看看经常面试的知识点,为什么面试这些知识点, 如果你是初级的或刚毕业的j ...
- android事件拦截处理机制具体解释
前段时间刚接触过android手机开发.对它的事件传播机制不是非常了解,尽管网上也查了相关的资料,可是总认为理解模模糊糊,似是而非,于是自己就写个小demo測试了一下. 总算搞明确了它的详细机制.写下 ...
- 用android studio创建第一个安卓程序加载html5 页面
前言 软件版本:android studio v1.0正式版,由于v0.x以来软件变化一直比较大,很多问题搜索的解决方案也都是v0.x版本时代的,故首先声明一下版本. 动机:由于工作中需要对移动端软件 ...
- Objective-C 和 Core Foundation 对象相互转换的内存管理总结
本文转载至 http://blog.csdn.net/allison162004/article/details/38756649 OS允许Objective-C 和 Core Foundation ...
- CAFFE学习笔记(四)将自己的jpg数据转成lmdb格式
1 引言 1-1 以example_mnist为例,如何加载属于自己的测试集? 首先抛出一个问题:在example_mnist这个例子中,测试集是人家给好了的.那么如果我们想自己试着手写几个数字然后验 ...
- vue项目在APP禁止页面缩放
veu-cli自动生成的项目中,index.html中meta 标签内容如下,放在手机上浏览 是可以放大缩小的<meta name="viewport" content=&q ...
- php, tp5, 选中导航菜单
1. 首先定义一个函数: function nav_select($navindex){ $nav_arr = [ 1 => ['index',], 2 => ['mei',], 3 =& ...
- CMDB的四种模式
为什么要有CMDB? CMDB --Configuration Management Database 配置管理数据库. 1.为了实现资产的自动采集,资产的自动更新, 为了搭建公司自动化平台的基础则需 ...