python 字典 注意点
dict()构造函数直接从键-值对序列创建字典:
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127}
此外,字典推导式式可以用于从任意键和值表达式创建字典:
>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}
如果键都是简单的字符串,
有时通过关键字参数指定 键-值 对更为方便:
>>> dict(sape=4139, guido=4127, jack=4098)
{'sape': 4139, 'jack': 4098, 'guido': 4127} 2.遍历技巧
使用enumerate(list)可以同时检索到序列的key和对应的value
for i,value in enumerate(list):
XXXX 对字典使用iteritems()同时取得键和对应的值。
同时遍历两个或更多的序列,
使用zip()函数可以成对读取元素。
>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
... print 'What is your {0}? It is {1}.'.format(q, a)
要反向遍历一个序列,
首先正向生成这个序列,
然后调用 reversed() 函数。
>>> for i in reversed(xrange(1,10,2)):
... print i
循环一个序列按排序顺序,
请使用sorted()函数,
返回一个新的排序的列表,
同时保留源不变。
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
... print f
若要在循环内部修改正在遍历的序列(例如复制某些元素),
建议您首先制作副本。
在序列上循环不会隐式地创建副本。
切片表示法使这尤其方便:
>>> words = ['cat', 'window', 'defenestrate']
>>> for w in words[:]: # Loop over a slice copy of the entire list.
... if len(w) > 6:
... words.insert(0, w)
...
>>> words
['defenestrate', 'cat', 'window', 'defenestrate']
python 字典 注意点的更多相关文章
- Python字典和集合
Python字典操作与遍历: 1.http://www.cnblogs.com/rubylouvre/archive/2011/06/19/2084739.html 2.http://5iqiong. ...
- python 字典排序 关于sort()、reversed()、sorted()
一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...
- python字典中的元素类型
python字典默认的是string item={"browser " : 'webdriver.irefox()', 'url' : 'http://xxx.com'} 如果这样 ...
- python字典copy()方法
python 字典的copy()方法表面看就是深copy啊,明显独立 d = {'a':1, 'b':2} c = d.copy() print('d=%s c=%s' % (d, c)) Code1 ...
- python 字典实现类似c的switch case
#python 字典实现类似c的switch def print_hi(): print('hi') def print_hello(): print('hello') def print_goodb ...
- python字典的常用操作方法
Python字典是另一种可变容器模型(无序),且可存储任意类型对象,如字符串.数字.元组等其他容器模型.本文章主要介绍Python中字典(Dict)的详解操作方法,包含创建.访问.删除.其它操作等,需 ...
- Python 字典(Dictionary)操作详解
Python 字典(Dictionary)的详细操作方法. Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 一.创建字典 字典由键和对应值成对组成.字 ...
- Python 字典(Dictionary) get()方法
描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...
- Python 字典(Dictionary) setdefault()方法
描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: ...
- python 字典内置方法get应用
python字典内置方法get应用,如果我们需要获取字典值的话,我们有两种方法,一个是通过dict['key'],另外一个就是dict.get()方法. 今天给大家分享的就是字典的get()方法. 这 ...
随机推荐
- Ztree的简单使用和后台交互的写法(二)
针对Ztree的简单使用和后台交互的写法(一)中的树进行改进 1.增加节点的权限 由页面的当前用户,决定树的根节点 然后动态获取树的详细节点: 初始化函数为: function init(){ //初 ...
- hdu 1318 Palindromes
Palindromes Time Limit:3000MS Memory Limit:0KB 64bit ...
- GLSL Interface Block参考
http://www.opengl.org/wiki/Interface_Block_(GLSL) http://stackoverflow.com/questions/9916103/opengl- ...
- input的onkeyup效果 超级简短代码
效果代码 title="请输入正确的十六位数字" onkeyup="javascript: return this.value = this.value.toUpperC ...
- adapter(转自Devin Zhang)
1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(ListView,GridView)等地方都需要用到Adapter.如下图直 ...
- 使用BeanNameAutoProxyCreator实现spring的自动代理
提到代理,我们可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames实现,但如果需要代理的bean很多,无疑会对spring配置 ...
- F5负载均衡的初识和基本配置
目前全球范围内应用比较广泛的负载均衡设备为美国的F5.F5于2000年底进驻中国,在国内业界,F5负载均衡产品已经成为了主流负载均衡技术的代名词.下面我们对F5负载均衡设备做一个基本介绍,方便大家去认 ...
- 新浪微博客户端(40)-使用AFN发送带图片的微博
DJComposeViewController.m /** 发微博 */ - (void)sendStatusRequest { AFHTTPSessionManager *RequestManage ...
- shell 脚本样例
1 解压文件,移动文件,删除特定目录 #!/bin/bash pa=$(cd ``; pwd) //获得当前目录的绝对路径 v_dir=${pa} mkdir ${v_dir} dirDist=${ ...
- Python程序的常见错误(收集篇)
关于Python Python是一门解释性的,面向对象的,并具有动态语义的高级编程语言.它高级的内置数据结构,结合其动态类型和动态绑定的特性,使得它在快速应用程序开发(Rapid Applicatio ...