Python2.6版本中实现字典推导 PEP 274(Dict Comprehensions)
之前自己也遇到过一次,这段时间在群里也遇到过几次的一个问题
用python2.7写的一段程序,里面用到了字典推导式,但是服务器版本是python2.6,无法运行。
今天查了下关于DictComprehensions,在pep274中有明确的说明。
http://legacy.python.org/dev/peps/pep-0274/
Implementation AllimplementationdetailswereresolvedinthePython2.7and3.0 time-frame.
这个是从2.7之后才加上的。
2.6版本中我们怎么用呢,其实用一个for循环来解决就好了
#表达式写法
In[4]:print{i:chr(65+i)foriinrange(4)}
{0:'A',1:'B',2:'C',3:'D'}
#for循环写法
In[5]:d={}
In[6]:foriinrange(4):
...: d[i]=chr(65+i)
...:
In[7]:printd
{0:'A',1:'B',2:'C',3:'D'}
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下