# Python 中的列表推导式

#### Python 中的列表推导式

• 简单用法
• 添加多个条件
• 多个列表使用
• 替换 Reduce 和 Lambda
• 嵌套列表推导式

``````new_list = [expression(item) for item in iterable if condition]
``````

``````new_list = []
for item in iterable:
if condition:
new_list.append(expression(item))
``````

## 简单用法

``````numbers = [x for x in range(1,11)]
print(numbers)
>>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
``````

``````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]
``````

``````# 如果 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]
``````

## 多个列表使用

``````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]]
``````

(0)