zoukankan      html  css  js  c++  java
  • Python中多层List展平为一层

    使用Python脚本的过程中,偶尔需要使用list多层转一层,又总是忘记怎么写搜索关键词,所以总是找了很久,现在把各种方法记录下来,方便自己也方便大家.

    方法很多,现在就简单写8种,后面再对这8种方法做基准测试.

    声明:文中的方法均收集自Making a flat list out of list of lists in Python


    1.定义减层方法

    1. import functools 
    2. import itertools 
    3. import numpy 
    4. import operator 
    5. import perfplot 
    6. from collections import Iterable # or from collections.abc import Iterable 
    7. from iteration_utilities import deepflatten 
    8.  
    9. #使用两次for循环 
    10. def forfor(a): 
    11. return [item for sublist in a for item in sublist] 
    12.  
    13. #通过sum 
    14. def sum_brackets(a): 
    15. return sum(a, []) 
    16.  
    17. #使用functools內建模块 
    18. def functools_reduce(a): 
    19. return functools.reduce(operator.concat, a) 
    20.  
    21. #使用itertools內建模块 
    22. def itertools_chain(a): 
    23. return list(itertools.chain.from_iterable(a)) 
    24.  
    25. #使用numpy 
    26. def numpy_flat(a): 
    27. return list(numpy.array(a).flat) 
    28.  
    29. #使用numpy 
    30. def numpy_concatenate(a): 
    31. return list(numpy.concatenate(a)) 
    32.  
    33. #自定义函数 
    34. def flatten(items): 
    35. """Yield items from any nested iterable; see REF.""" 
    36. for x in items: 
    37. if isinstance(x, Iterable) and not isinstance(x, (str, bytes)): 
    38. yield from flatten(x) 
    39. else: 
    40. yield x 
    41.  
    42. def pylangs_flatten(a): 
    43. return list(flatten(a)) 
    44.  
    45. #使用库iteration_utilities 
    46. def iteration_utilities_deepflatten(a): 
    47. return list(deepflatten(a, depth=1)) 

    2.测试

    1. a=[[1,2,3],[4,5,6],[7,8,9]] 
    2. print(a) 
    3.  
    4. print('--------------------------') 
    5.  
    6. print(forfor(a)) 
    7. print(sum_brackets(a)) 
    8. print(functools_reduce(a)) 
    9. print(itertools_chain(a)) 
    10. print(numpy_flat(a)) 
    11. print(numpy_concatenate(a)) 
    12. print(pylangs_flatten(a)) 
    13. print(iteration_utilities_deepflatten(a)) 

    输出:

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


    2.各种方法的基准测试(消耗时间对比)

    各种方法在小数据上消耗时间差别不大,如果数据很小,没必要为了选择而烦恼,如果数据很大,可以参考下面基准测试的结果来选择减层方法.

    1. import matplotlib.pyplot as plt 
    2. from simple_benchmark import benchmark 
    3.  
    4. #基准测试 
    5. b = benchmark( 
    6. [forfor, sum_brackets, functools_reduce, itertools_chain,numpy_flat, numpy_concatenate, pylangs_flatten,iteration_utilities_deepflatten], 
    7. arguments={2**i: [[0]*5]*(2**i) for i in range(1, 13)}, 
    8. argument_name='number of inner lists' 
    9. ) 
    10.  
    11. #显示测试结果 
    12. plt.subplots(1,1,figsize=(15,10)) 
    13. b.plot() 
    14. plt.legend(loc = 'upper left') 
    15. plt.show() 

    消耗时间对比
    消耗时间对比

    相同数据量,纵轴方向越小,方法越快.


    代码可以从这里下载,需要部署Jupyter环境,可参考我的博客部署方法.

  • 相关阅读:
    zoj 4120Tokens on the Segments(优先队列+贪心)
    hdu1710 Binary Tree Traversals(二叉树)
    poj3494Largest Submatrix of All 1’s
    poj 2559Largest Rectangle in a Histogram(单调栈简单模板题)
    poj 2492 A Bug's Life(种类并查集)
    差分约束 + spfa + 最长路 [NOI1999] 01串
    Codeforces Round #599 D Yet Another Monster Killing Problem
    CF 1249D1
    [Gym-102346A] 偷偷偷 并查集处理图(坐标)
    [Gym-102346M] 二分答案
  • 原文地址:https://www.cnblogs.com/wushaogui/p/9241931.html
Copyright © 2011-2022 走看看