列表推導式入門到精通

2022-06-25 12:24:06 字數 3099 閱讀 7367

#

一般形式

mylist_one=[x for x in range(1,100,2)]#

輸出1-100的奇數

mylist_two=[x+1 for x in range(1,100,2)]#

輸出1-100的偶數

print(mylist_one,'

mylist_one

');print(mylist_two,'

mylist_two')

##一般形式+判斷

mylist_one=[x for x in range(1,100,2) if x >50]#

輸出50之後的奇數

mylist_two=[x+1 for x in range(1,100,2) if x >50]#

輸出50之後的偶數

print(mylist_one,'

mylist_one

');print(mylist_two,'

mylist_two')

#列表巢狀列表

mylist_one=[[x,x+1,x*x] for x in range(1,100,2)]#

輸出50之後的奇數

print(mylist_one,'

mylist_one')

#無限迴圈巢狀

mylist_one=[[x,y] for x in range(10) for y in range(10)]#

輸出50之後的奇數

print(mylist_one,'

mylist_one

')

輸出結果:

1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99] mylist_one

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100] mylist_two

[51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99] mylist_one

[52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100] mylist_two

[[1, 2, 1], [3, 4, 9], [5, 6, 25], [7, 8, 49], [9, 10, 81], [11, 12, 121], [13, 14, 169], [15, 16, 225], [17, 18, 289], [19, 20, 361], [21, 22, 441], [23, 24, 529], [25, 26, 625], [27, 28, 729], [29, 30, 841], [31, 32, 961], [33, 34, 1089], [35, 36, 1225], [37, 38, 1369], [39, 40, 1521], [41, 42, 1681], [43, 44, 1849], [45, 46, 2025], [47, 48, 2209], [49, 50, 2401], [51, 52, 2601], [53, 54, 2809], [55, 56, 3025], [57, 58, 3249], [59, 60, 3481], [61, 62, 3721], [63, 64, 3969], [65, 66, 4225], [67, 68, 4489], [69, 70, 4761], [71, 72, 5041], [73, 74, 5329], [75, 76, 5625], [77, 78, 5929], [79, 80, 6241], [81, 82, 6561], [83, 84, 6889], [85, 86, 7225], [87, 88, 7569], [89, 90, 7921], [91, 92, 8281], [93, 94, 8649], [95, 96, 9025], [97, 98, 9409], [99, 100, 9801]] mylist_one

[[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [0, 8], [0, 9], [1, 0], [1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [2, 9], [3, 0], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [3, 6], [3, 7], [3, 8], [3, 9], [4, 0], [4, 1], [4, 2], [4, 3], [4, 4], [4, 5], [4, 6], [4, 7], [4, 8], [4, 9], [5, 0], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5], [5, 6], [5, 7], [5, 8], [5, 9], [6, 0], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7], [6, 8], [6, 9], [7, 0], [7, 1], [7, 2], [7, 3], [7, 4], [7, 5], [7, 6], [7, 7], [7, 8], [7, 9], [8, 0], [8, 1], [8, 2], [8, 3], [8, 4], [8, 5], [8, 6], [8, 7], [8, 8], [8, 9], [9, 0], [9, 1], [9, 2], [9, 3], [9, 4], [9, 5], [9, 6], [9, 7], [9, 8], [9, 9]] mylist_one

python快速入門(列表推導式)

其實哈,我感覺這個應該不太算入門的要求了,只是有時候小夥伴們可能會碰到有些for迴圈語句只有一行,但是實際上功能十分強大 o 重點是 相當簡潔,然而可讀性就稍微差一些啦。所以,對於接觸python時間不長的我們來說,看到列表推導式還是蠻痛苦的。小夥伴們可以先瞅一眼下面的 有乙個規律 列表推導式總是放...

列表推導式

1.先判斷再賦值 a 5 b 6 c a b print c false 列表組合 list1 1,2,3 list2 4,5,6 list3 list1 list2 print list3 一行 方括號中是對字串的切片 後邊有個or 為從左到右,取第乙個為真的值,如果or左側為非空字串,就是用or...

列表推導式

0 9的平方,輸出為列表 list1 x 2 for x in range 10 range 10 是0 9 print list1 結果為 0,1,4,9,16,25,36,49,64,81 使用for迴圈完成 0 9的平方,輸出為列表 list1 for i in range 10 print ...