zoukankan      html  css  js  c++  java
  • Python之List列表如何处理最好?正确案例详解

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

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


    大家要注意光理论是不够的。这里顺便总大家一套2020最新python入门到高级项目实战视频教程,可以去小编的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)

    最后大家要注意光理论是不够的。这里顺便总大家一套2020最新python入门到高级项目实战视频教程,可以去小编的Python交流.裙 :七衣衣九七七巴而五(数字的谐音)转换下可以找到了,还可以跟老司机交流讨教!

    本文的文字及图片来源于网络加上自己的想法,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

  • 相关阅读:
    子类继承父类,必须声明父类的构造函数
    开发识别屏幕方向的应用程序
    PDA智能设备解决方案打包及部署
    Xcode 下cocos-2dx 环境搭建
    PO订单审批拒绝API
    PO订单审批通过API
    Oracle Forms Services Architecture
    《Oracle Applications DBA 基础》- 9
    Useful Articles for the Oracle E-Business Suite Technology Stack for Technical Consultant / DBA [ID
    Understanding and Using HRMS Security in Oracle HRMS
  • 原文地址:https://www.cnblogs.com/chengxuyuanaa/p/12884453.html
Copyright © 2011-2022 走看看