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

倒置 list

  •  
  •   frmongo · Aug 6, 2018 · 2744 views
    This topic created in 2872 days ago, the information mentioned may be changed or developed.
    如果一个 list 的元素都是随机字符串,如何对 list 倒置,注意没有排序哦, 只是顺序换了?实现越短越好
    9 replies    2018-08-07 10:21:57 +08:00
    ballshapesdsd
        1
    ballshapesdsd  
       Aug 6, 2018
    啥意思[::-1]不行么
    Yourshell
        2
    Yourshell  
       Aug 6, 2018 via iPhone
    list 不是有 reverse 方法吗
    twor
        3
    twor  
       Aug 6, 2018
    >>> a = [1,2,4,3,4]
    >>> a.reverse()
    >>> a
    [4, 3, 4, 2, 1]
    >>> list(reversed(a))
    [1, 2, 4, 3, 4]
    >>> a[::-1]
    [1, 2, 4, 3, 4]
    >>>
    neosfung
        4
    neosfung  
       Aug 6, 2018 via Android
    我觉得楼主说的是数据结构里的 list 吧
    xpresslink
        5
    xpresslink  
       Aug 6, 2018
    请楼使用人类易于易于理解的语言说明一下倒置代表的寒意。
    tt67wq
        6
    tt67wq  
       Aug 7, 2018
    数据结构挂科生
    frmongo
        7
    frmongo  
    OP
       Aug 7, 2018
    @tt67wq 确实挂科了,层主
    frmongo
        8
    frmongo  
    OP
       Aug 7, 2018
    @xpresslink sorry,语文也挂了..+_+
    frmongo
        9
    frmongo  
    OP
       Aug 7, 2018
    抱歉各位,py 基础不扎实,专门来碰钉子,我在 b = a.reverse()了一个 list 后,发现 b 是 none.
    总结了下,reverse()方法会修改原来的对象,[::-1]切片可以得到新的对象。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5026 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 09:06 · PVG 17:06 · LAX 02:06 · JFK 05:06
    ♥ Do have faith in what you're doing.