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

list 里边按 index 插入 list 并展开?有啥好方法吗

  •  
  •   css3 · Aug 6, 2019 · 2396 views
    This topic created in 2611 days ago, the information mentioned may be changed or developed.

    想把一个 list 插入到另一个 list 的倒数第二位并展开。有什么简单的办法不

    a = [1,2,3,4]
    
    b = ["a", "b"]
    
    c = [1,2,3,"a","b",4] #如何让 c 等于这样?
    
    6 replies  •  2019-08-07 16:29:14 +08:00
    zhusimaji
        1
    zhusimaji  
       Aug 6, 2019 via Android
    a[:-1]+b+a[-1]
    cassidyhere
        2
    cassidyhere  
       Aug 6, 2019
    a[-2:-1] = b
    Abirdcfly
        3
    Abirdcfly  
       Aug 7, 2019
    c = a[:-1] + b + a[-1]
    或者
    a[-1:0]=b
    xpresslink
        4
    xpresslink  
       Aug 7, 2019
    二楼+100086
    sladesha
        5
    sladesha  
       Aug 7, 2019
    a[-1:-1] = b 即可
    hongda0812
        6
    hongda0812  
       Aug 7, 2019
    也可取出 list 遍历也行
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3800 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 05:34 · PVG 13:34 · LAX 22:34 · JFK 01:34
    ♥ Do have faith in what you're doing.