手机
当前位置:查字典教程网 >脚本专栏 >python >Python2.6版本中实现字典推导 PEP 274(Dict Comprehensions)
Python2.6版本中实现字典推导 PEP 274(Dict Comprehensions)
摘要:之前自己也遇到过一次,这段时间在群里也遇到过几次的一个问题用python2.7写的一段程序,里面用到了字典推导式,但是服务器版本是pytho...

之前自己也遇到过一次,这段时间在群里也遇到过几次的一个问题

用python2.7写的一段程序,里面用到了字典推导式,但是服务器版本是python2.6,无法运行。

今天查了下关于Dict Comprehensions,在pep274中有明确的说明。

http://legacy.python.org/dev/peps/pep-0274/

复制代码 代码如下:

Implementation

All implementation details were resolved in the Python 2.7 and 3.0

time-frame.

这个是从2.7之后才加上的。

2.6版本中我们怎么用呢,其实用一个for循环来解决就好了

复制代码 代码如下:

#表达式写法

In [4]: print {i : chr(65+i) for i in range(4)}

{0: 'A', 1: 'B', 2: 'C', 3: 'D'}

复制代码 代码如下:

#for循环写法

In [5]: d = {}

In [6]: for i in range(4):

...: d[i] = chr(65+i)

...:

In [7]: print d

{0: 'A', 1: 'B', 2: 'C', 3: 'D'}

【Python2.6版本中实现字典推导 PEP 274(Dict Comprehensions)】相关文章:

Python中字典(dict)和列表(list)的排序方法实例

python实现百度关键词排名查询

Python实现端口复用实例代码

Python 第一步 hello world

python 将字符串转换成字典dict

Python版的文曲星猜数字游戏代码

Python科学计算环境推荐——Anaconda

Python 字典(Dictionary)操作详解

python中将字典转换成其json字符串

python笔记(2)

精品推荐
分类导航