Python 中的列表推导式
列表推导式是一种 Python 构造,可减少生成新列表或过滤现有列表所需的代码行。列表推导式包含在方括号内,它由一个表达式、一个或多个 for 循环和一个用于过滤生成的列表的可选条件组成。
列表推导式使用以下语法:
new_list = [expression(item) for item in iterable if condition]
翻译翻译就是:
new_list = []
for item in iterable:
if condition:
new_list.append(expression(item))
简单用法
最原始的1到10
numbers = [x for x in range(1,11)]
print(numbers)
>>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
乘个2
numbers = [2*x for x in range(1,11)]
print(numbers)
>>> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
只要偶数
numbers = [x for x in range(1,11) if x%2==0]
print(numbers)
>>> [2, 4, 6, 8, 10]
来个等差数列
numbers = [2*x-1 for x in range(1,11)]
print(numbers)
>>> [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
来个二维数组
numbers = [[x, x**2] for x in range(2,7)]
print(numbers)
>>> [[2, 4], [3, 9], [4, 16], [5, 25], [6, 36]]
添加多个条件
只要一个范围内
numbers = [2*x for x in range(1,11) if x>2 and x<5]
print(numbers)
>>> [6, 8]
要添加 else 条件,我们必须重新排列列表理解元素的顺序。一个三元表达式的顺序
我们必须将条件移动到 for 关键字之前,以便在不满足 if 条件时返回一个不同于 2*x 的值。
# 如果 x 的值介于2和5之间,则列表推导式返回 2*x,否则返回 3*x。
numbers = [2*x if x > 2 and x < 5 else 3*x for x in range(10)]
print(numbers)
>>> [0, 3, 6, 6, 8, 15, 18, 21, 24, 27]
如果条件在多一点
numbers = []
for x in range(10):
if x > 2 and x < 5:
numbers.append(2*x)
elif x <=2:
numbers.append(3*x)
else:
numbers.append(4*x)
print(numbers)
>>> [0, 3, 6, 6, 8, 20, 24, 28, 32, 36]
numbers = [2*x if x > 2 and x < 5 else 3*x if x <=2 else 4*x for x in range(10)]
print(numbers)
>>> [0, 3, 6, 6, 8, 20, 24, 28, 32, 36]
多个列表使用
与 zip 函数一起使用的列表推导返回一个元组列表,其中第 n 个元组包含每个列表的第 n 个元素。
cities = ['Rome', 'Warsaw', 'London']
countries = ['Italy', 'Poland', 'United Kingdom']
abc = [(city, country) for city, country in zip(cities, countries)]
print(abc)
>>> [('Rome', 'Italy'), ('Warsaw', 'Poland'), ('London', 'United Kingdom')]
cities = ['Rome', 'Warsaw', 'London']
countries = ['Italy', 'Poland', 'United Kingdom']
languages = ['Italian', 'Polish', 'English']
abc = [(city, country, language) for city, country, language in zip(cities, countries, languages)]
print(abc)
>>> [('Rome', 'Italy', 'Italian'), ('Warsaw', 'Poland', 'Polish'), ('London', 'United Kingdom', 'English')]
下面演示了两种不同的加法。挨个加一遍,,对应加,,
a = [2, 3, 4, 5]
b = [12, 13, 14, 15]
abc = [x+y for x in a for y in b]
print(abc)
>>> [14, 15, 16, 17, 15, 16, 17, 18, 16, 17, 18, 19, 17, 18, 19, 20]
a = [2, 3, 4, 5]
b = [12, 13, 14, 15]
abc = [x+y for x,y in zip(a,b)]
print(abc)
>>> [14, 16, 18, 20]
三个也是一样的
a = [2, 3]
b = [12, 13]
c = [22, 23]
abc = [x+y+z for x in a for y in b for z in c]
print(abc)
>>> [36, 37, 37, 38, 37, 38, 38, 39]
a = [2, 3, 4, 5]
b = [12, 13, 14, 15]
c = [22, 23, 24, 25]
abc = [x+y+z for x,y,z in zip(a,b,c)]
print(abc)
>>> [36, 39, 42, 45]
替换 Reduce 和 Lambda
reduce 函数返回总和
from functools import reduce
numbers = [3, 6, 8, 23]
print(reduce(lambda a,b: a+b, numbers))
>>> 40
修改一下
print(sum([number for number in numbers]))
>>> 40
嵌套列表推导式
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for row in matrix:
for item in row:
item *= 2
print(matrix)
>>> [[2, 4, 6], [8, 10, 12], [14, 16, 18]]
简单地来个转换
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
matrix = [[item*2 for item in row] for row in matrix]
print(matrix)
>>> [[2, 4, 6], [8, 10, 12], [14, 16, 18]]