将大数组里面的小数组平行展开的实现(Making a flat list out of list of lists in Python)

时间:2022-02-27 19:33:19

今天在生成数据的时候遇到了这个需求,其实写一个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://*.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python

http://*.com/questions/406121/flattening-a-shallow-list-in-python

将大数组里面的小数组平行展开的实现(Making a flat list out of list of lists in Python)的更多相关文章

  1. C# 大数组赋值给小数组,小数组赋值给大数组

    ]; ]; " }; arraymax = arraystr;//变成和arraystr一样 arraymin = arraystr;//变成和arraystr一样

  2. 算法---数组总结篇2——找丢失的数,找最大最小,前k大,第k小的数

    一.如何找出数组中丢失的数 题目描述:给定一个由n-1个整数组成的未排序的数组序列,其原始都是1到n中的不同的整数,请写出一个寻找数组序列中缺失整数的线性时间算法 方法1:累加求和 时间复杂度是O(N ...

  3. [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 ...

  4. PHP开发小技巧③—实现多维数组转化为一维数组

    在平常的项目开发中我们多会用到让多维数组转化为一维数组的情况,但是很多Programmer不会将其进行转化,也有些没有想到很好的算法然后经过乱起八糟的运算方式将其勉强转化好,但是所写的程序代码冗余非常 ...

  5. java-IO流-字节流-概述及分类、FileInputStream、FileOutputStream、available()方法、定义小数组、BufferedInputStream、BufferedOutputStream、flush和close方法的区别、流的标准处理异常代码

    1.IO流概述及其分类 * 1.概念      * IO流用来处理设备之间的数据传输      * Java对数据的操作是通过流的方式      * Java用于操作流的类都在IO包中      *  ...

  6. js限制输入数字能输入小数点,js定义数组,js往数组中添加数据,js将字符型转为数字型,除法结果保留两位小数——js小测:计算比赛得分

    一个朋友跟我说要去给某个比赛算分: 规则:去掉最低分最高分求平均分: 最近在学习大数据可视化——图谱,用到js一些东西,所以今天就用js练练 用到知识点: js限制输入数字能输入小数点,js定义数组, ...

  7. 小数组的读写和带Buffer的读写哪个快

    定义小数组如果是8192个字节大小和Buffered比较的话 定义小数组会略胜一筹,因为读和写操作的是同一个数组 而Buffered操作的是两个数组

  8. Golang字符串是否存在于切片或数组中的小工具(基本等同于python in语法)

    // golang中是没有判断字符串是否存在数组或切片里的内置方法, 所以我们可以自己搞一个快速小组件 func Find(slice []string, val string) (int, bool ...

  9. js操作数组的一些小技巧

    1.从数组中随机获取成员 var items = [12, 548 , 'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' , 2145 , 119]; var  rando ...

随机推荐

  1. Looper

    /** * Class used to run a message loop for a thread. Threads by default do * not have a message loop ...

  2. 用ActionSupport实现验证

    第一种: 只要Action类继承了ActionSupport,就可以用验证方案了 是个原始的方案,需要自己写代码,但是很灵活,登陆案例 不足:业务处理和验证的代码混在一起,不方便验证部分的复用和维护 ...

  3. hdu 2000

    ps:刚开始学C++...用C++来试试.. 代码: #include <iostream> using namespace std; int main(){ ],t,i,j; ]> ...

  4. KindEditor - 代码高亮

    *:插入数据库的数据,不用转义,KE已经自动转义过了. 调用的时候使用引入代码的css: 显示代码的页面:

  5. No plugin found for prefix &&num;39&semi;tomcat&&num;39&semi; in the current project and in the plugin groups和java&period;net&period;BindException&colon; Address already in use&colon; JVM&lowbar;Bind &lt&semi;null&gt&semi;&colon;8080的错误解决

    错误报告:No plugin found for prefix 'tomcat' in the current project and in the plugin groups [org.apache ...

  6. 【Java入门提高篇】Day28 Java容器类详解(十)LinkedHashMap详解

    今天来介绍一下容器类中的另一个哈希表———>LinkedHashMap.这是HashMap的关门弟子,直接继承了HashMap的衣钵,所以拥有HashMap的全部特性,并青出于蓝而胜于蓝,有着一 ...

  7. 12&colon; xlrd 处理Excel文件

    1.1 xlrd处理.xlsx 文件 1.xlrd常用方法 #!/usr/bin/python # coding:utf-8 # 用xlrd读取Excel文件基本用法 import sys impor ...

  8. PAT 甲级 1011 World Cup Betting &lpar;20&rpar;(20 分)

    1011 World Cup Betting (20)(20 分)提问 With the 2010 FIFA World Cup running, football fans the world ov ...

  9. C语言 &&num;183&semi; 最长单词

    算法提高 最长单词   时间限制:1.0s   内存限制:512.0MB      编写一个函数,输入一行字符,将此字符串中最长的单词输出. 输入仅一行,多个单词,每个单词间用一个空格隔开.单词仅由小 ...

  10. 如何检查CentOS服务器受到DDOS攻击

    登录到你的服务器以root用户执行下面的命令,使用它你可以检查你的服务器是在DDOS攻击与否: netstat -anp |grep 'tcp\|udp' | awk '{print $5}' | c ...