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
css3
V2EX  ›  Python

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

  •  
  •   css3 · 2019-08-06 16:35:53 +08:00 · 1394 次点击
    这是一个创建于 1716 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    a = [1,2,3,4]
    
    b = ["a", "b"]
    
    c = [1,2,3,"a","b",4] #如何让 c 等于这样?
    
    6 条回复    2019-08-07 16:29:14 +08:00
    zhusimaji
        1
    zhusimaji  
       2019-08-06 16:40:19 +08:00 via Android
    a[:-1]+b+a[-1]
    cassidyhere
        2
    cassidyhere  
       2019-08-06 17:33:32 +08:00
    a[-2:-1] = b
    Abirdcfly
        3
    Abirdcfly  
       2019-08-07 04:32:59 +08:00
    c = a[:-1] + b + a[-1]
    或者
    a[-1:0]=b
    xpresslink
        4
    xpresslink  
       2019-08-07 09:53:17 +08:00
    二楼+100086
    sladesha
        5
    sladesha  
       2019-08-07 16:24:49 +08:00
    a[-1:-1] = b 即可
    hongda0812
        6
    hongda0812  
       2019-08-07 16:29:14 +08:00
    也可取出 list 遍历也行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5444 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 07:10 · PVG 15:10 · LAX 00:10 · JFK 03:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.