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)文章
Python3自帶工具2to3.py 轉(zhuǎn)換 Python2.x 代碼到Python3的操作
Python3自帶工具2to3.py 轉(zhuǎn)換 Python2.x 代碼到Python3的操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03python django事務(wù)transaction源碼分析詳解
這篇文章主要介紹了python django事務(wù)transaction源碼分析詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03基于python實(shí)現(xiàn)監(jiān)聽Rabbitmq系統(tǒng)日志代碼示例
這篇文章主要介紹了基于python實(shí)現(xiàn)監(jiān)聽Rabbitmq系統(tǒng)日志代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11python調(diào)用matplotlib模塊繪制柱狀圖
這篇文章主要為大家介紹了python調(diào)用matplotlib模塊繪制柱狀圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10Python?Asyncio庫(kù)之a(chǎn)syncio.task常用函數(shù)詳解
Asyncio在經(jīng)過(guò)一段時(shí)間的發(fā)展以及獲取Curio等第三方庫(kù)的經(jīng)驗(yàn)來(lái)提供更多的功能,目前高級(jí)功能也基本完善。本文主要介紹了Asyncio庫(kù)中asyncio.task常用函數(shù)的使用,需要的可以參考一下2023-03-03對(duì)pycharm代碼整體左移和右移縮進(jìn)快捷鍵的介紹
今天小編就為大家分享一篇對(duì)pycharm代碼整體左移和右移縮進(jìn)快捷鍵的介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07