comprehensions --> 理解,故又称列表理解式,提供了一种简明扼要的方法[]来创建列表。它的结构是在一个中括号里包含一个表达式,然后是一个for语句,然后是0个或多个for或者if语句。那个表达式可以是任意的,意思是你可以在列表中放入任意类型的对象。返回结果将是一个新的列表,在这个以if和for语句为上下文的表达式运行完成之后产生。
list_variable = [x for x in iterable]
In [2]: list1 = [ x for x in range(1,11)]
...: print(list1)
...:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
推导式中可以调用 函数,这里 使用函数 int() 最后转化 int 类型 并 推导生成新的列表
In [11]: feet = ['12', '1024', '724', '996']
In [12]: feet = [int(x) for x in feet]
In [13]: print(feet)
[12, 1024, 724, 996]
正整数 1 到 10 之间,如果 能被 2 整除,则返回 此数字 的平方 输出列表
In [4]: new_list = [n**2 for n in range(1,11) if n%2==0]
In [5]: print(new_list)
[4, 16, 36, 64, 100]
正整数 0 到 99 之间,如果 能被 2 整除 同时 又能被 6 整除 ,则返回 生成 新列表
In [16]: divided = [x for x in range(100) if x % 2 == 0 if x % 6 == 0]
In [17]: print(divided)
[0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, 96]
正整数 1 到 10 之间,如果数字 x 大于等于 7 则返回 x+1 ,否则 返回 x+5 ,返回生成新的列表
In [26]: new_list = [x+1 if x >= 7 else x+5 for x in range(1, 11)]
In [27]: print(new_list)
[6, 7, 8, 9, 10, 11, 8, 9, 10, 11]
找到嵌套列表中名字含有两个‘e’的所有名字
In [40]: names = [['Tom', 'Billy', 'Jefferson', 'Andrew', 'Wesley', 'Steven', 'Joe'],
...: ['Alice', 'Jill', 'Ana', 'Wendy', 'Jennifer', 'Sherry', 'Eva']]
In [41]: new_list = [name for elements in names for name in elements if name.count('e') >= 2]
In [42]: print(new_list)
['Jefferson', 'Wesley', 'Steven', 'Jennifer']
嵌套列表中,大列表内包含 多个 子列表,通过列表推导生成式 输出所有元素生成到 新的 大列表内
In [33]: list_of_list = [[1,2,3],[4,5,6],[7,8]]
# Flatten `list_of_list`
In [34]: new_list = [y for x in list_of_list for y in x]
In [35]: print(new_list)
[1, 2, 3, 4, 5, 6, 7, 8]
矩阵 matrix 中 三个 小列表,现将 每个列元素 取出, 推导生成一个 新列表矩阵
In [48]: matrix = [[1,2,3],[4,5,6],[7,8,9]]
In [49]: new_list = [[row[i] for row in matrix] for i in range(3)]
In [50]: print(new_list)
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
dict_variable = {key:value for (key, value) in iterable}
dict_variable = {key:value for (key,value) in dictonary.items()}
In [51]: dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
# Double each value in the dictionary
In [52]: double_dict1 = {k:v*2 for (k,v) in dict1.items()}
In [53]: print(double_dict1)
{'a': 2, 'b': 4, 'c': 6, 'd': 8, 'e': 10}
In [59]: dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
In [60]: dict1_keys = {k*2:v for (k,v) in dict1.items()}
In [61]: print(dict1_keys)
{'aa': 1, 'bb': 2, 'cc': 3, 'dd': 4, 'ee': 5}
numbers = range(10)
new_dict_for = {}
# Add values to `new_dict` using for loop
for n in numbers:
if n%2==0:
new_dict_for[n] = n**2
print(new_dict_for)
输出:
{0: 0, 2: 4, 4: 16, 6: 36, 8: 64}
In [65]: new_dict_for = {n:n**2 for n in range(10) if n%2 == 0 }
In [66]: print(new_dict_for)
{0: 0, 2: 4, 4: 16, 6: 36, 8: 64}
dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
# Check for items greater than 2
dict1_cond = {k:v for (k,v) in dict1.items() if v>2}
print(dict1_cond)
输出:
{'e': 5, 'c': 3, 'd': 4}
In [70]: dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
In [71]: dict1_doubleCond = {k:v for (k,v) in dict1.items() if v>2 if v%2 == 0}
In [72]: print(dict1_doubleCond)
{'d': 4}
In [73]: dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f':6}
In [74]: dict1_tripleCond = {k:v for (k,v) in dict1.items() if v>2 if v%2 == 0 if v%3 == 0}
In [75]: print(dict1_tripleCond)
{'f': 6}
写成 普通 循环来看 如下:
dict1_tripleCond = {}
for (k,v) in dict1.items():
if (v>=2 and v%2 == 0 and v%3 == 0):
dict1_tripleCond[k] = v
print(dict1_tripleCond)
In [76]: dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f':6}
# Identify odd and even entries
In [77]: dict1_tripleCond = {k:('even' if v%2==0 else 'odd') for (k,v) in dict1.items()}
In [78]: print(dict1_tripleCond)
{'a': 'odd', 'b': 'even', 'c': 'odd', 'd': 'even', 'e': 'odd', 'f': 'even'}
In [81]: nested_dict = {'first':{'a':1}, 'second':{'b':2}}
In [82]: float_dict = {outer_k: {float(inner_v) for (inner_k, inner_v) in outer_v.items()} for (outer_k, outer_v) in nested_dict.items()}
In [83]: print(float_dict)
{'first': {1.0}, 'second': {2.0}}
nested_dict = {'first':{'a':1}, 'second':{'b':2}}
for (outer_k, outer_v) in nested_dict.items():
for (inner_k, inner_v) in outer_v.items():
outer_v.update({inner_k: float(inner_v)})
nested_dict.update({outer_k:outer_v})
print(nested_dict)
输出:
{'first': {'a': 1.0}, 'second': {'b': 2.0}}
In [85]: squared = {x**2 for x in [1 , -1, 2]}
In [86]: print(squared)
{1, 4}