Python的列表推導(dǎo)式你了解嗎
語(yǔ)法
1.普通
[expression for target in iterable]
2.帶條件
[expression for target in iterable if condition]
3.嵌套
[expression for target1 in iterable1 if condition1 for target2 in iterable2 if condition2 ... for targetN in iterableN if conditionN]
實(shí)例
x = [1, 2, 3, 4, 5] x = [i * 2 for i in x] print(x) # [2, 4, 6, 8, 10]
y = [i for i in range(10)] print(y) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# 獲取矩陣第二列
matrix = [
[1, 2, 3],
[3, 5, 6],
[7, 8, 9]
]
col2 = [row[1] for row in matrix]
print(col2)
# [2, 5, 8]
# 獲取矩陣主對(duì)角線元素
matrix = [
[1, 2, 3],
[3, 5, 6],
[7, 8, 9]
]
diag = [matrix[i][i] for i in range(len(matrix))]
print(diag)
diag2 = [matrix[i][len(matrix) - i - 1] for i in range(len(matrix))]
print(diag2)
# [1, 5, 9]
# [3, 5, 7]
# 創(chuàng)建內(nèi)嵌列表 a = [[0] * 3 for i in range(3)] print(a) a[1][1] = 5 print(a) # [[0, 0, 0], [0, 0, 0], [0, 0, 0]] # [[0, 0, 0], [0, 5, 0], [0, 0, 0]]
# 20以內(nèi)偶數(shù)列表 b = [i for i in range(20) if i % 2 == 0] print(b) # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
# 篩選F開頭的單詞
words = ['Great', 'FishC', 'Brilliant', 'Excellent', 'Fantistic']
res = [word for word in words if word.startswith('F')]
print(res)
# ['FishC', 'Fantistic']
# 展開二維列表
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
flatten = [col for row in matrix for col in row ]
print(flatten)
# [1, 2, 3, 4, 5, 6, 7, 8, 9]
等價(jià)于
flatten = [] for row in matrix: for col in row: flatten.append(col)
# 笛卡爾積 fulljoin = [x + y for x in '12345' for y in 'abcde'] print(fulljoin) # ['1a', '1b', '1c', '1d', '1e', '2a', '2b', '2c', '2d', '2e', '3a', '3b', '3c', '3d', '3e', '4a', '4b', '4c', '4d', '4e', '5a', '5b', '5c', '5d', '5e']
ans = [[x, y] for x in range(10) if x % 2 == 0 for y in range(10) if y % 3 == 0] print(ans) # [[0, 0], [0, 3], [0, 6], [0, 9], [2, 0], [2, 3], [2, 6], [2, 9], [4, 0], [4, 3], [4, 6], [4, 9], [6, 0], [6, 3], [6, 6], [6, 9], [8, 0], [8, 3], [8, 6], [8, 9]]
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
- Python之列表推導(dǎo)式最全匯總(上篇)
- Python之列表推導(dǎo)式最全匯總(中篇)
- python中列表推導(dǎo)式與生成器表達(dá)式對(duì)比詳解
- Python列表推導(dǎo)式,元組推導(dǎo)式,字典推導(dǎo)式,集合推導(dǎo)式
- python列表推導(dǎo)式的原理及使用方法
- python列表推導(dǎo)式實(shí)現(xiàn)找出列表中長(zhǎng)度大于5的名字
- Python 列表推導(dǎo)式與字典推導(dǎo)式的實(shí)現(xiàn)
- Python列表推導(dǎo)式詳情
- python列表推導(dǎo)式 經(jīng)典代碼
- Python 列表推導(dǎo)式需要注意的地方
- Python列表推導(dǎo)式實(shí)現(xiàn)代碼實(shí)例
- 什么是python的列表推導(dǎo)式
- python列表推導(dǎo)式入門學(xué)習(xí)解析
- python之列表推導(dǎo)式的用法
- python列表推導(dǎo)式操作解析
- python 列表推導(dǎo)式使用詳解
- 簡(jiǎn)單了解python 生成器 列表推導(dǎo)式 生成器表達(dá)式
- Python之列表推導(dǎo)式最全匯總(下篇)
相關(guān)文章
Python curses內(nèi)置顏色用法實(shí)例
在本篇文章里小編給大家整理的是一篇關(guān)于Python curses內(nèi)置顏色用法實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-06-06
pip安裝py_zipkin時(shí)提示的SSL問(wèn)題對(duì)應(yīng)
今天小編就為大家分享一篇關(guān)于pip安裝py_zipkin時(shí)提示的SSL問(wèn)題對(duì)應(yīng),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Python中的imread()函數(shù)用法說(shuō)明
這篇文章主要介紹了Python中的imread()函數(shù)用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
python圖片剪裁代碼(圖片按四個(gè)點(diǎn)坐標(biāo)剪裁)
這篇文章主要介紹了python圖片剪裁代碼(圖片按四個(gè)點(diǎn)坐標(biāo)剪裁),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
基于Django OneToOneField和ForeignKey的區(qū)別詳解
這篇文章主要介紹了基于Django OneToOneField和ForeignKey的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
完美解決Pycharm無(wú)法導(dǎo)入包的問(wèn)題 Unresolved reference
今天小編就為大家分享一篇完美解決Pycharm無(wú)法導(dǎo)入包的問(wèn)題 Unresolved reference,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
wxPython中wx.gird.Gird添加按鈕的實(shí)現(xiàn)
本文主要介紹了wxPython中wx.gird.Gird添加按鈕的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03

