关于字典iteritems() ,用于遍历 字典的key 和value 只适用于 python 2.x ,在python 3.x 中就是 items()
In [7]: for key,value in b_dict.iteritems():
...: print key,'=>',value,
...:
0 => a 1 => b 2 => c 3 => d 4 => e 5 => f 6 => g 7 => h 8 => i 9 => j
In [7]: for key,value in b_dict.iteritems():
...: print key,'=>',value,
...:
0 => a 1 => b 2 => c 3 => d 4 => e 5 => f 6 => g 7 => h 8 => i 9 => j
2017-07-29
In [1]: import string
In [2]: string.letters
Out[2]: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
In [3]: a_list = range(10)
In [4]: a_list
Out[4]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [5]: b_dict = {i: string.letters[i] for i in a_list}
In [6]: b_dict
In [2]: string.letters
Out[2]: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
In [3]: a_list = range(10)
In [4]: a_list
Out[4]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [5]: b_dict = {i: string.letters[i] for i in a_list}
In [6]: b_dict
2017-07-29
匿名函数
func = lambda(x :x*2)
列表/字典生成式 也即 列表字典推导式 ,别的地方是叫 生成式的 一般是 [] 结构,而生成器则是()结构
func = lambda(x :x*2)
列表/字典生成式 也即 列表字典推导式 ,别的地方是叫 生成式的 一般是 [] 结构,而生成器则是()结构
2017-07-29