推荐学习书目
› 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
ZoeYn
V2EX  ›  Python

有没有比较优雅的合并两个列表的方法?

  •  
  •   ZoeYn · Apr 22, 2021 · 4453 views
    This topic created in 2020 days ago, the information mentioned may be changed or developed.
    a = [1,2]
    b = [a,b,c,f]

    l = [[1,a],[1,b],[1,c],[1,f],[2,a],[2,b],[2,c],[2,f]]

    请问,除了双层 for 循环之外还有别的办法吗?
    19 replies  •  2021-04-28 09:06:17 +08:00
    cassidyhere
    cassidyhere
    Apr 22, 2021
    1
    from itertools import product
    product(a, b)
    abersheeran
    abersheeran
    Apr 22, 2021 via Android❤️8
    2
    你这不叫合并,你这是算笛卡尔积。循环是少不了的,但是写法可以不一样。用 map 或者推导式。
    ZoeYn
    ZoeYn
    OP
    Apr 22, 2021
    3
    @cassidyhere 试了一下,可以!!
    ZoeYn
    ZoeYn
    OP
    Apr 22, 2021
    4
    @abersheeran 搜嘎!我去补一补这个知识,谢谢!
    JackCooper
    JackCooper
    Apr 22, 2021
    5
    ruby 的话

    2.5.7 :001 > a = [1,2,3,4]
    => [1, 2, 3, 4]
    2.5.7 :002 > b = ['a', 'b' ,'c', 'd']
    => ["a", "b", "c", "d"]
    2.5.7 :003 > a.zip(b)
    => [[1, "a"], [2, "b"], [3, "c"], [4, "d"]]
    hyrious
    hyrious
    Apr 22, 2021
    6
    @JackCooper a.product(b)
    encounter2017
    encounter2017
    Apr 22, 2021❤️1
    7
    python 的话

    from itertools import product
    a = [1,2]
    b = ['a','b','c']

    list(product(a,b))
    # [(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c')]

    scala 的话

    scala> for {
    | a <- List(1, 2)
    | b <- List('a', 'b', 'c')
    | } yield (a,b)
    val res1: List[(Int, Char)] = List((1,a), (1,b), (1,c), (2,a), (2,b), (2,c))
    bxb100
    bxb100
    Apr 22, 2021
    8
    Numpy 不是一堆矩阵方法吗
    cyspy
    cyspy
    Apr 22, 2021
    10
    [(x, y) for x in a for y in b]
    Lordon
    Lordon
    Apr 22, 2021
    11
    可以参考 leetcode17 题 形式上很像
    css3
    css3
    Apr 22, 2021
    12
    ret = [[str(x) + y] for x in a for y in b]
    ZoeYn
    ZoeYn
    OP
    Apr 23, 2021
    13
    @css3
    @Lordon
    @cyspy
    @Codewj
    @bxb100
    @encounter2017
    @hyrious
    @JackCooper
    好嘞,谢谢各位前辈~
    raaaaaar
    raaaaaar
    Apr 23, 2021
    14
    都是 O(n^2) 的,数据库连接操作就以这个为基础,如果能优化数据库早干了
    liuxingdeyu
    liuxingdeyu
    Apr 23, 2021
    15
    这。。。笛卡尔积无解,就得一个一个算,无非就是写法上优雅点,map 、reduce 、lambda 啥的
    HashV2
    HashV2
    Apr 23, 2021
    16
    itertools 库看一遍吧,方法不多 但是都很好用
    sugarkeek
    sugarkeek
    Apr 23, 2021
    17
    就算是库也是这么一个一个合并的
    ZoeYn
    ZoeYn
    OP
    Apr 28, 2021
    18
    @raaaaaar
    @liuxingdeyu
    @sugarkeek
    好吧 哈哈哈这个好像确实没办法
    ZoeYn
    ZoeYn
    OP
    Apr 28, 2021
    19
    @HashV2 嗯嗯嗯 我之前还不知道这个库,,一直写俩循环来着
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1707 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 23:42 · PVG 07:42 · LAX 15:42 · JFK 18:42
    ♥ Do have faith in what you're doing.