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

Python 多个列表相加怎么写

  •  
  •   JCZ2MkKb5S8ZX9pq · Oct 22, 2020 · 4270 views
    This topic created in 2164 days ago, the information mentioned may be changed or developed.
    # 伪数据。从 excel 返回内容,字典形式,表名是 key,内容是 value 。内容里每行一个 json 。
    data = read_excel(a)
    
    excel 的内容是 
    [
      [表 1 的各行
        {1a:1,1b:2,1c:3,...},
        {1a2:1,1b2:2,1c2:3,...},
        ...
      ],
      [表 2 的各行],
      ...,
      [表 N 的各行]
    ]
    也就是一个 2 层的数组
    
    现在想获取的是,把第二层都拼成一个数组。也就是类似
    itertools.chain(**data)
    
    或者
    new = []
    for sheet in data:
      new += sheet
    

    还有什么写法比较好?

    15 replies  •  2020-10-23 07:25:43 +08:00
    airdge
        1
    airdge  
       Oct 22, 2020
    sum(data.values())
    EggtartZ
        2
    EggtartZ  
       Oct 22, 2020
    意思是要把一个列表的字典合并成一个字典?
    cryingsky
        3
    cryingsky  
       Oct 22, 2020
    flatten
    hmdsw
        4
    hmdsw  
       Oct 22, 2020
    pandas
    TimePPT
        5
    TimePPT  
       Oct 22, 2020
    字典粗暴合并如果存在两个字典有一样的 key 会丢数据吧,除非合并后同 key 的 value 合并成列表之类的。那这样还是两层。
    sudoy
        6
    sudoy  
       Oct 22, 2020   ❤️ 1
    ```
    >>> x = {'a': 1, 'b': 2}
    >>> y = {'b': 3, 'c': 4}
    >>> z = {**x, **y}
    >>> z
    {'c': 4, 'a': 1, 'b': 3}
    ```
    iblislsy
        7
    iblislsy  
       Oct 22, 2020   ❤️ 1
    sum([[1,2,3],[3,4,5]],[])
    这题完结
    JCZ2MkKb5S8ZX9pq
        8
    JCZ2MkKb5S8ZX9pq  
    OP
       Oct 22, 2020
    @iblislsy 原来 sum 还有第二个参数,一直没这么用过。
    JCZ2MkKb5S8ZX9pq
        9
    JCZ2MkKb5S8ZX9pq  
    OP
       Oct 22, 2020
    @iblislsy

    如果不加第二个参数,会报类型错误,这个怎么理解?
    我本来感觉,它是把第一个参数的 list[0] 作为初始值,然后 list[0]+list[1]+...+list[n]的。但好像一定要带上第二个 start 参数,定义一个空列表?
    BBCCBB
        10
    BBCCBB  
       Oct 22, 2020
    sum 的第二个参数默认为 0,, 不指定的话就是 0, 所以类型不匹配.
    wuwukai007
        11
    wuwukai007  
       Oct 22, 2020 via Android
    list(chain(*youlist))
    from itertools import chain
    kaitolucifer
        12
    kaitolucifer  
       Oct 22, 2020 via iPhone
    The issue of quadratic performance of sum(sequences, null_seq) is known, which is why the doc says that sum() is for numbers and recommends .join for strings and itertools.chain for other sequences.

    上面那个 sum trick 貌似很慢?
    owtotwo
        13
    owtotwo  
       Oct 22, 2020
    itertools.chain.from_iterable 应该可以?
    lithbitren
        14
    lithbitren  
       Oct 22, 2020
    印象中测过 chain 最快,sum 次之,循环最慢
    billgreen1
        15
    billgreen1  
       Oct 23, 2020
    建议考虑一下 pandas
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1261 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 17:00 · PVG 01:00 · LAX 10:00 · JFK 13:00
    ♥ Do have faith in what you're doing.