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 · 4617 次点击
    这是一个创建于 3617 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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('reduce(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   ·   实用小工具   ·   1170 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:31 · PVG 07:31 · LAX 16:31 · JFK 19:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.