欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python的列表推導(dǎo)式你了解嗎

 更新時(shí)間:2022年03月22日 16:31:24   作者:吳聲子夜歌  
這篇文章主要為大家詳細(xì)介紹了Python的列表推導(dǎo)式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助

語(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)容!  

相關(guān)文章

  • Python3自帶工具2to3.py 轉(zhuǎn)換 Python2.x 代碼到Python3的操作

    Python3自帶工具2to3.py 轉(zhuǎn)換 Python2.x 代碼到Python3的操作

    Python3自帶工具2to3.py 轉(zhuǎn)換 Python2.x 代碼到Python3的操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-03-03
  • python django事務(wù)transaction源碼分析詳解

    python 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)日志代碼示例

    這篇文章主要介紹了基于python實(shí)現(xiàn)監(jiān)聽Rabbitmq系統(tǒng)日志代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Python 中 list 的各項(xiàng)操作技巧

    Python 中 list 的各項(xiàng)操作技巧

    最近在學(xué)習(xí) python 語(yǔ)言。大致學(xué)習(xí)了 python 的基礎(chǔ)語(yǔ)法。覺得 python 在數(shù)據(jù)處理中的地位和它的 list 操作密不可分,今天把相關(guān)基礎(chǔ)操作記錄到腳本之家平臺(tái),需要的的朋友參考下
    2017-04-04
  • python調(diào)用matplotlib模塊繪制柱狀圖

    python調(diào)用matplotlib模塊繪制柱狀圖

    這篇文章主要為大家介紹了python調(diào)用matplotlib模塊繪制柱狀圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • Python?Asyncio庫(kù)之a(chǎn)syncio.task常用函數(shù)詳解

    Python?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
  • 詳解python中的hashlib模塊的使用

    詳解python中的hashlib模塊的使用

    這篇文章主要介紹了python中的hashlib模塊的使用,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-04-04
  • Python打印三角形九九乘法表代碼

    Python打印三角形九九乘法表代碼

    大家好,本篇文章主要講的是Python打印三角形九九乘法表代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • 對(duì)pycharm代碼整體左移和右移縮進(jìn)快捷鍵的介紹

    對(duì)pycharm代碼整體左移和右移縮進(jìn)快捷鍵的介紹

    今天小編就為大家分享一篇對(duì)pycharm代碼整體左移和右移縮進(jìn)快捷鍵的介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • 利用pandas讀取中文數(shù)據(jù)集的方法

    利用pandas讀取中文數(shù)據(jù)集的方法

    今天小編就為大家分享一篇利用pandas讀取中文數(shù)據(jù)集的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07

最新評(píng)論