Python List 列表的处理

建立日期: 2020/04/13

更新日期: 2020/04/16

说明:本文请随意引用或更改,只须标示出处及作者,作者不保证内容絶对正确无误,如造成任何后果,请自行负责.

Python List 列表的处理

本文主要在于一些非常态的列表处理, 至于Python list 自带的一些函数或方法, 请见下方Python 列表常用方法.

相关的方法会持续续加进来, 也希望读者有一些方式不知道怎么用的,或者有其他的方法, 敬请提示.

  1. 对列表各元素, 逐一处理
>>> 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]
  1. 条件选择/删除元素
  • 使用 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']
  1. 展平列表
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]
  1. 列表转换
  • 列表与字典
>>> a = [[1,2],[3,4]]
>>> b = dict(a)
>>> b
{1: 2, 3: 4}
>>> list(map(list, b.items()))
[[1, 2], [3, 4]]
  1. 列表的排列组合
>>> 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], [1, 5], [1, 6], [2, 4], [2, 5], [2, 6], [3, 4], [3, 5], [3, 6]]
>>> list(map(list, product(a, repeat=2)))
[[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
  1. 列表转置
>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> list(map(list, zip(*a)))
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]


Python 列表常用方法

  1. 建立列表
  • 直接建立
>>> [0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
  • 函数/方法/产生器建立
>>> [i for i in range(5)]
[0, 1, 2, 3, 4]
  • 一维双变量交互建立
>>> [(i, j*10) for i in range(5) for j in range(5)]
[(0, 0), (0, 10), (0, 20), (0, 30), (0, 40), (1, 0), (1, 10), (1, 20), ( 1, 30), (1, 40), (2, 0), (2, 10), (2, 20), (2, 30), (2, 40), (3, 0), (3, 10), (3, 20), (3, 30), (3, 40), (4, 0), (4, 10), (4, 20), (4, 30), (4, 40) ]
  • 一维双变量对应建立
>>> [(i, j*10) for i, j in zip(range(5), range(5))]
[(0, 0), (1, 10), (2, 20), (3, 30), (4, 40)]
  • 多维列表建立
>>> [[[i, j] for i in range(5)] for j in range(5)]
[[[0, 0], [1, 0], [2, 0], [3, 0], [4, 0]],
 [[0, 1], [1, 1], [2, 1], [3, 1], [4, 1]],
 [[0, 2], [1, 2], [2, 2], [3, 2], [4, 2]],
 [[0, 3], [1, 3], [2, 3], [3, 3], [4, 3]],
 [[0, 4], [1, 4], [2, 4], [3, 4], [4, 4]]]
  • 从其他变量建立
>>> list('We are the world')
['W', 'e', ' ', 'a', 'r', 'e', ' ', 't', 'h', 'e', ' ', 'w', 'o', 'r', 'l', 'd']
>>> list(range(5))
[0, 1, 2, 3, 4]
  1. 修改列表
  • 单一元素修改
>>> a = [1, 2, 3, 5]
>>> a[3] = 4
>>> a
[1, 2, 3, 4]
  • 部份修改
>>> a[1:3] = [5, 6]
[1, 5, 6, 4]
>>> a[1:3] = [8]
[1, 8, 4]
  1. 复制列表
  • 记忆体参考位置复制
>>> a = [1, 2, 3, 4]
>>> b = a
>>> b[1] = 10
>>> a, b
([1, 10, 3, 4], [1, 10, 3, 4])
  • 一维浅层复制
>>> from copy import copy, deepcopy
>>> a = [[1,2,3], [4, 5,6]]
>>> b = copy(a) # 同 b = a[:]
>>> b[0] = [7, 8, 9] # 第一层修改不会改变被复制对象
>>> a, b
([[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [4, 5, 6]])
>>> b[0][0] = 100 # 第二层修改会改变被复制对象
>>> a, b
([[1, 2, 3], [100, 5, 6]], [[7, 8, 9], [100, 5, 6]]
  • 多维深层复制, 完全不会改变被复制对象
>>> a = [[1,2,3], [4, 5,6]]
>>> b = deepcopy(a)
>>> b[0] = [7, 8, 9]
>>> b[1][0] = 100
>>> a, b
([[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [100, 5, 6]])
  1. 增加列表内容
  • 插入
>>> a = [1, 2, 3, 4]
>>> a.insert(0, 0)
[0, 1, 2, 3, 4]
>>> a.insert(2, 12)
[0, 1, 12, 2, 3, 4]
>>> a.insert(100, 99)
[0, 1 ,12, 2, 3, 4, 99]
  • 插入多列
>>> a[2:2] = [20, 21, 22]
[0, 1, 20, 21, 22, 12, 2, 3, 4, 99]
  • 后面附上
>>> a = [0]
>>> a = a.append(1)
>>> a = a.append(2)
[0, 1, 2]
  • 重复内容
>>> a = [1,2] * 5
[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
  • 列表组合
>>> a, b = [1, 2], [3, 4]
>>> a + b
[1, 2, 3, 4]
>>> c = a.extend(b)
>>> a, c
([1, 2, 3, 4], None)
  1. 删除列表内容
  • 以索引来删除
>>> a = [1, 2, 3, 4, 3]
>>> b = a.pop(0)
>>> a, b
([2, 3, 4, 3], 1)
  • 以值来删除第一个
>>> a.remove(3)
[2, 4, 3]
  • 直接删除
>>> del a[1:]
[2]
  1. 列表的其他方法
  • 计数该值出现的次数
>>> a = [1, 3, 3, 5, 3, 2, 6, 3]
>>> a.count(3)
4
  • 返回该值的第一個索引
>>> a.index(5)
3
  • 列表排序, 不修改
>>> c = sorted(a)
>>> a, c
([1, 3, 3, 5, 3, 2, 6, 3], [1, 2, 3, 3, 3, 3, 5, 6])
  • 列表排序, 自修改
>>> c = a.sort()
>>> a, c
([1, 2, 3, 3, 3, 3, 5, 6], None)
  • 倒序
>>> a = [1, 2, 3, 4]
>>> a[::-1]
[4, 3, 2, 1]
>>> c = a.reverse()
>>> a, c
([4, 3, 2, 1], None)
本作品采用《CC 协议》,转载必须注明作者和本文链接
Jason Yang
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!