列表推導(dǎo)式是一種 Python 構(gòu)造,可減少生成新列表或過(guò)濾現(xiàn)有列表所需的代碼行。列表推導(dǎo)式包含在方括號(hào)內(nèi),它由一個(gè)表達(dá)式、一個(gè)或多個(gè) for 循環(huán)和一個(gè)用于過(guò)濾生成的列表的可選條件組成。
列表推導(dǎo)式使用以下語(yǔ)法:
new_list = [expression(item) for item in iterable if condition]
翻譯翻譯就是:
new_list = []
for item in iterable:
if condition:
new_list.append(expression(item))
簡(jiǎn)單用法
最原始的1到10
numbers = [x for x in range(1,11)]
print(numbers)
>>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
乘個(gè)2
numbers = [2*x for x in range(1,11)]
print(numbers)
>>> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
只要偶數(shù)
numbers = [x for x in range(1,11) if x%2==0]
print(numbers)
>>> [2, 4, 6, 8, 10]
來(lái)個(gè)等差數(shù)列
numbers = [2*x-1 for x in range(1,11)]
print(numbers)
>>> [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
來(lái)個(gè)二維數(shù)組
numbers = [[x, x**2] for x in range(2,7)]
print(numbers)
>>> [[2, 4], [3, 9], [4, 16], [5, 25], [6, 36]]
添加多個(gè)條件
只要一個(gè)范圍內(nèi)
numbers = [2*x for x in range(1,11) if x>2 and x<5]
print(numbers)
>>> [6, 8]
要添加 else 條件,我們必須重新排列列表理解元素的順序。一個(gè)三元表達(dá)式的順序
我們必須將條件移動(dòng)到 for 關(guān)鍵字之前,以便在不滿足 if 條件時(shí)返回一個(gè)不同于 2*x 的值。
# 如果 x 的值介于2和5之間,則列表推導(dǎo)式返回 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]
如果條件在多一點(diǎn)
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]
多個(gè)列表使用
與 zip 函數(shù)一起使用的列表推導(dǎo)返回一個(gè)元組列表,其中第 n 個(gè)元組包含每個(gè)列表的第 n 個(gè)元素。
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')]
下面演示了兩種不同的加法。挨個(gè)加一遍,,對(duì)應(yīng)加,,
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]
三個(gè)也是一樣的
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 函數(shù)返回總和
from functools import reduce
numbers = [3, 6, 8, 23]
print(reduce(lambda a,b: a+b, numbers))
>>> 40
修改一下文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-740367.html
print(sum([number for number in numbers]))
>>> 40
嵌套列表推導(dǎo)式
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]]
簡(jiǎn)單地來(lái)個(gè)轉(zhuǎn)換文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-740367.html
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]]
到了這里,關(guān)于Python 中的列表推導(dǎo)式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!