将大数组里面的小数组平行展开的实现(Making a flat list out of list of lists in Python)
今天在生成数据的时候遇到了这个需求,其实写一个for循环可以很容易解决这个问题,但是无论是性能还是酷炫程度上都不行 所以顺手搜索了一下。
例子是将
l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
变成
[1, 2, 3, 4, 5, 6, 7, 8, 9]
plan1: 使用列表推导式
print [item for i in l for item in i]
plan2: 使用reduce
print reduce(lambda x, y: x + y, l)
print reduce(operator.concat, l)
plan3: 使用itertool
print list(itertools.chain.from_iterable(l))
plan4: 使用sum
print sum(l, [])
那么,哪种方法最快呢? timeit!
import timeit
print timeit.timeit('reduce(lambda x, y: x + y, l)', setup='l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]', number=10000)
print timeit.timeit('reduce(operator.concat, l)', setup='import operator;'
'l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]', number=10000)
print timeit.timeit('[item for i in l for item in i]', setup='l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]', number=10000)
print timeit.timeit('list(itertools.chain.from_iterable(l))', setup='import itertools;'
'l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]', number=10000)
print timeit.timeit('sum(l, [])', setup='l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]', number=10000)
output:
0.0129590034485
0.00949192047119
0.0104038715363
0.0122821331024
0.0097348690033
可以看到,速度其实都差不多,在一个数量级上,但是第二个操作一般会更快 也就是使用operator的operator.concat
Reference:
http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python
http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python
将大数组里面的小数组平行展开的实现(Making a flat list out of list of lists in Python)的更多相关文章
- C# 大数组赋值给小数组,小数组赋值给大数组
]; ]; " }; arraymax = arraystr;//变成和arraystr一样 arraymin = arraystr;//变成和arraystr一样
- 算法---数组总结篇2——找丢失的数,找最大最小,前k大,第k小的数
一.如何找出数组中丢失的数 题目描述:给定一个由n-1个整数组成的未排序的数组序列,其原始都是1到n中的不同的整数,请写出一个寻找数组序列中缺失整数的线性时间算法 方法1:累加求和 时间复杂度是O(N ...
- [LeetCode] Split Array With Same Average 分割数组成相同平均值的小数组
In a given integer array A, we must move every element of A to either list B or list C. (B and C ini ...
- PHP开发小技巧③—实现多维数组转化为一维数组
在平常的项目开发中我们多会用到让多维数组转化为一维数组的情况,但是很多Programmer不会将其进行转化,也有些没有想到很好的算法然后经过乱起八糟的运算方式将其勉强转化好,但是所写的程序代码冗余非常 ...
- java-IO流-字节流-概述及分类、FileInputStream、FileOutputStream、available()方法、定义小数组、BufferedInputStream、BufferedOutputStream、flush和close方法的区别、流的标准处理异常代码
1.IO流概述及其分类 * 1.概念 * IO流用来处理设备之间的数据传输 * Java对数据的操作是通过流的方式 * Java用于操作流的类都在IO包中 * ...
- js限制输入数字能输入小数点,js定义数组,js往数组中添加数据,js将字符型转为数字型,除法结果保留两位小数——js小测:计算比赛得分
一个朋友跟我说要去给某个比赛算分: 规则:去掉最低分最高分求平均分: 最近在学习大数据可视化——图谱,用到js一些东西,所以今天就用js练练 用到知识点: js限制输入数字能输入小数点,js定义数组, ...
- 小数组的读写和带Buffer的读写哪个快
定义小数组如果是8192个字节大小和Buffered比较的话 定义小数组会略胜一筹,因为读和写操作的是同一个数组 而Buffered操作的是两个数组
- Golang字符串是否存在于切片或数组中的小工具(基本等同于python in语法)
// golang中是没有判断字符串是否存在数组或切片里的内置方法, 所以我们可以自己搞一个快速小组件 func Find(slice []string, val string) (int, bool ...
- js操作数组的一些小技巧
1.从数组中随机获取成员 var items = [12, 548 , 'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' , 2145 , 119]; var rando ...
随机推荐
- oracle等待事件-direct path read/write
转://http://blog.chinaunix.net/uid-23177306-id-2531235.html 一.direct path read1.与直接读取相关联的等待事件.当ORACLE ...
- oracle RAC 查询告警日志位置
[grid@db2 db2]$ adrci ADRCI: Release 12.2.0.1.0 - Production on Mon Feb 25 15:51:14 2019 Copyright ( ...
- php 数组元素快速去重
1.使用array_unique方法进行去重 对数组元素进行去重,我们一般会使用array_unique方法,使用这个方法可以把数组中的元素去重. <?php $arr = array(,,,, ...
- 2小时学会Spring Boot(IDE:eclipse)
一:安装STS插件 官网下载:点此下载STS 注意:STS版本必须与eclipse版本对应 安装教程:http://blog.csdn.net/cryhelyxx/article/details/53 ...
- MyBatis动态SQL(认真看看, 以后写SQL就爽多了)
目录 0 一起来学习 mybatis 1 数据准备 2 if 标签 2.1 在 WHERE 条件中使用 if 标签 2.1.1 查询条件 2.1.2 动态 SQL 2.1.3 测试 2.2 在 UPD ...
- gohost -- go 开发的命令行hosts配置管理工具
前几天在微博上看到有人推荐了lazygit这个工具,让人眼前一亮,什么时候命令行也可以这么抢到了,
- Leetcode 665. Non-decreasing Array(Easy)
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- Hadoop生态的配置
网盘下载地址 链接: https://pan.baidu.com/s/19qWnP6LQ-cHVrvT0o1jTMg 密码: 44hs Hadoop伪分布式配置 Hadoop 可以在单节点上以伪分布 ...
- Applese 的毒气炸弹 G 牛客寒假算法基础集训营4(图论+最小生成树)
链接:https://ac.nowcoder.com/acm/contest/330/G来源:牛客网 Applese 的毒气炸弹 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262 ...
- Springboot+Redis 配置和使用
pom.xml 引入redis 开启缓存 <!-- cache --> <dependency> <groupId>org.springframework.boot ...