本文主要在於一些非常態的列表處理,至於 Python list 自帶的一些函數或方法,請見下方 Python 列表常用方法.
相關的方法會持續續加進來,也希望讀者有一些方式不知道怎么用的,或者有其他的方法,敬請提示.
大家要注意:光理論是不夠的。這里順便總大家一套2020最新python入門到高級項目實戰視頻教程,可以去小編的Python交流.裙 :七衣衣九七七巴而五(數字的諧音)轉換下可以找到了,還可以跟老司機交流討教!
- 對列表各元素,逐一處理
>>> import math >>> a = [math.sqrt(i) for i in range(10)] [0.0, 1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979, 2.449489742783178, 2.6457513110645907, 2.8284271247461903, 3.0] >>> b = [list(range(1,5)), list(range(5,9)), list(range(9,13))] [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
- 建立列表直接處理
>>> # 一維方式 >>> [int(i*10)/10 for i in a] [0.0, 1.0, 1.4, 1.7, 2.0, 2.2, 2.4, 2.6, 2.8, 3.0] >>> # 二維方式 >>> [[b[j][i]**2 for i in range(len(b[j]))] for j in range(len(b))] [[1, 4, 9, 16], [25, 36, 49, 64], [81, 100, 121, 144]]
- 使用函數,可以重復使用,也適合處理更復雜的狀況
>>> def def func(sequence): ... return [int(i*10)/10 for i in sequence] >>> print(func(a)) [0.0, 1.0, 1.4, 1.7, 2.0, 2.2, 2.4, 2.6, 2.8, 3.0]
- 使用 map + 函數,可以簡化函數的處理,更可適用在單一變量或列表
>>> def func(i): ... return int(i*10)/10 >>> list(map(func, a)) [0.0, 1.0, 1.4, 1.7, 2.0, 2.2, 2.4, 2.6, 2.8, 3.0] >>> func(1.41459) 1.4
- 使用 map + lambda 方法,可使 map 對應多參數的函數
>>> list(map(lambda i:int(i*10)/10, a)) [0.0, 1.0, 1.4, 1.7, 2.0, 2.2, 2.4, 2.6, 2.8, 3.0]
- 使用 map+lambda+function
>>> def Map(func, sequence, *argc): ... return list(map(lambda i:func(i, *argc), sequence)) >>> Map(round, a, 2) [0.0, 1.0, 1.41, 1.73, 2.0, 2.24, 2.45, 2.65, 2.83, 3.0] >>> Map(int, a) [0, 1, 1, 1, 2, 2, 2, 2, 2, 3]
- 條件選擇 / 刪除元素
- 使用 if 條件,建立新列表
>>> # 一維方式 >>> [i for i in a if 2<i<3] [2.23606797749979, 2.449489742783178, 2.6457513110645907, 2.8284271247461903]
- 使用 filter + function
>>> def func(i): ... return 2<i<3 >>> list(filter(func, a)) [2.23606797749979, 2.449489742783178, 2.6457513110645907, 2.8284271247461903]
- 使用 filter + lambda
>>> list(filter(lambda i: 2<i<3, a)) [2.23606797749979, 2.449489742783178, 2.6457513110645907, 2.8284271247461903]
- 使用 filter + lambda + function
>>> def Filter(func, sequence, *argc): ... return list(filter(lambda i:func(i, *argc), sequence)) >>> Filter(float.__gt__, a, 2) # 大於2的 >>> b = ['This', 'There', 'Where', 'Here'] >>> Filter(str.__contains__, b, 'T') # 字串中有'T'的 ['This', 'There'] >>> Filter(str.__contains__, b, 'r') # 字串中有'r'的 ['There', 'Where', 'Here']
- 展平列表
def Flat_List(sequence): result = [] if isinstance(sequence, list): for item in sequence: if isinstance(item, list): result += Flat_List(item) else: result.append(item) return result else: return sequence Flat_List([[1,2,[3,4,5],6,[7,8,[9,10],[11,12]]]]) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
- 列表轉換
- 列表與字典
>>> a = [[1,2],[3,4]] >>> b = dict(a) >>> b {1: 2, 3: 4} >>> list(map(list, b.items())) [[1, 2], [3, 4]]
- 列表的排列組合
>>> a = [1,2,3] >>> [[i, j] for i in a for j in a] [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]] >>> [[i, j] for i in a for j in a if i!=j] [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]] >>> from itertools import permutations, combinations, product >>> list(map(list, permutations(a, 2))) [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]] >>> list(map(list, combinations(a, 2))) [[1, 2], [1, 3], [2, 3]] >>> b = [4, 5, 6] >>> list(map(list, product(a, b))) [[1, 4],