如何一行代码实现将两个数组交叉 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
gkiwi
V2EX    Python

如何一行代码实现将两个数组交叉

  •  
  •   gkiwi 2014-06-16 18:06:40 +08:00 5073 次点击
    这是一个创建于 4147 天前的主题,其中的信息可能已经有所发展或是发生改变。
    t=[1,2,3]
    m=['a','b','c']

    两数组长度一致;
    希望一行代码优雅的将数组一一交叉,得到类似如下结果:

    [1,'a',2,'b',3,'c']
    25 条回复    2014-06-17 10:39:06 +08:00
    xingxiucun
        1
    xingxiucun  
       2014-06-16 18:17:42 +08:00   2
    reduce(lambda x, y: x + y, zip(a, b))
    tonyluj
        2
    tonyluj  
       2014-06-16 18:18:35 +08:00   1
    想到一个(python):
    [item for elem in zip([1,2,3],['a','b','c']) for item in elem]
    gkiwi
        3
    gkiwi  
    OP
       2014-06-16 18:24:45 +08:00
    @xingxiucun
    @tonyluj
    谢谢两位.
    phuslu
        4
    phuslu  
       2014-06-16 18:25:40 +08:00   1
    我这个效率比较低,但是稍微短一些

    sum(map(list, zip(t, m)), [])
    ddzz
        5
    ddzz  
       2014-06-16 18:29:48 +08:00 via Android   1
    第一反应就是循环其中一个数组,然后将他们元素逐个加到一个新的数组,怎么实现不重要,能实现就行
    gkiwi
        6
    gkiwi  
    OP
       2014-06-16 18:30:01 +08:00
    @phuslu sum原来还能这样用...学习啦!
    gkiwi
        7
    gkiwi  
    OP
       2014-06-16 18:31:25 +08:00
    @ddzz 我也只会酱紫的...zip被卡住了~
    hanks315
        8
    hanks315  
       2014-06-16 18:34:03 +08:00
    [item for elem in zip(a, b) for item in elem]
    ddzz
        9
    ddzz  
       2014-06-16 18:35:59 +08:00 via Android
    @gkiwi 如果经常在各种语言中切换,笨方法往往是最好的。这里的笨方法是那种省脑子,但稍微累点手指的方法
    gkiwi
        10
    gkiwi  
    OP
       2014-06-16 18:46:31 +08:00 via Android
    @ddzz 已经被语言切换到杂乱!!
    gkiwi
        11
    gkiwi  
    OP
       2014-06-16 18:48:32 +08:00 via Android
    @hanks315 谢谢。
    xieranmaya
        12
    xieranmaya  
       2014-06-16 18:48:43 +08:00
    _.flatten(_.zip([1,2,3],['a','b','c']))
    JS的
    xieranmaya
        13
    xieranmaya  
       2014-06-16 18:49:06 +08:00
    咦,这里好像是py板块...
    hit9
        14
    hit9  
       2014-06-16 18:55:33 +08:00   1
    来个sum的:

    >>> sum(zip(t,m), tuple())
    (1, 'a', 2, 'b', 3, 'c')
    9hills
        15
    9hills  
       2014-06-16 19:15:25 +08:00
    1l好评,效率也不错,reduce是迭代调用的
    gkiwi
        16
    gkiwi  
    OP
       2014-06-16 20:39:54 +08:00
    @xieranmaya 你乱入了...话说flatten,zip,_,是自带的函数?我查了查也没找到.用什么库了?
    gkiwi
        17
    gkiwi  
    OP
       2014-06-16 20:40:42 +08:00
    @hit9 学习了!
    Actrace
        18
    Actrace  
       2014-06-16 20:43:16 +08:00   1
    一行代码是指调用一个函数(指令)还是写成一行?
    不管是不是一个函数(指令),又或者纯靠语法,一行实现都没啥问题啊。
    013231
        19
    013231  
       2014-06-16 20:53:18 +08:00   1
    @xingxiucun @9hills 使用中的itertools.chain, 效率高很多.

    In [690]: a = range(1000)

    In [691]: b = range(1000)

    In [692]: timeit reduce(lambda x, y: x + y, zip(a, b))
    100 loops, best of 3: 3.25 ms per loop

    In [693]: timeit list(itertools.chain(*zip(x, y)))
    10000 loops, best of 3: 110 s per loop
    9hills
        20
    9hills  
       2014-06-16 21:18:26 +08:00 via iPad   1
    @013231 感觉上面最大的是函数调用的开销
    chlx
        21
    chlx  
       2014-06-16 22:25:22 +08:00   1
    @013231 我的机子上的结果.

    In [18]: timeit list(itertools.chain(*zip(t,m)))
    1000000 loops, best of 3: 773 ns per loop

    In [19]: timeit reduce(lambda x, y: x + y, zip(t, m))
    1000000 loops, best of 3: 549 ns per loop
    013231
        22
    013231  
       2014-06-16 22:26:46 +08:00   1
    @9hills 如此.

    In [719]: a = range(100000)

    In [720]: b = range(100000)

    In [721]: cProfile.run('redce(lambda x, y: x + y, zip(a, b))')
    100003 function calls in 46.933 seconds

    Ordered by: standard name

    ncalls tottime percall cumtime percall filename:lineno(function)
    99999 27.569 0.000 27.569 0.000 <string>:1(<lambda>)
    1 0.005 0.005 46.933 46.933 <string>:1(<module>)
    1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
    1 19.341 19.341 46.911 46.911 {reduce}
    1 0.017 0.017 0.017 0.017 {zip}



    In [722]: cProfile.run('list(itertools.chain(*zip(a, b)))')
    3 function calls in 0.022 seconds

    Ordered by: standard name

    ncalls tottime percall cumtime percall filename:lineno(function)
    1 0.014 0.014 0.022 0.022 <string>:1(<module>)
    1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
    1 0.008 0.008 0.008 0.008 {zip}
    013231
        23
    013231  
       2014-06-16 22:29:34 +08:00
    @chlx 你用的t, m非常小吧.
    xieranmaya
        24
    xieranmaya  
       2014-06-17 09:53:06 +08:00
    gkiwi
        25
    gkiwi  
    OP
       2014-06-17 10:39:06 +08:00
    @xieranmaya 谢谢.看了下,功能很强大,万能的下划线'_' .
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3199 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 33ms UTC 10:48 PVG 18:48 LAX 03:48 JFK 06:48
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86