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
  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. plt.subplots(1,1,figsize=(15,10)) 
  12. b.plot() 
  13. plt.legend(loc = 'upper left'
  14. plt.show() 

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

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


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

转载于:https://www.cnblogs.com/wushaogui/p/9241931.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值