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

我想问一下这是为什么?

  •  
  •   YangJian1994 · Apr 4, 2018 · 4646 views
    This topic created in 3151 days ago, the information mentioned may be changed or developed.
    a = ['1123dsfd', 'fdgt565', '345r']
    b = a.sort()
    为什么 b 得到的是 None ?不是很明白。
    18 replies  •  2018-04-09 10:11:35 +08:00
    ulala
    ulala
    Apr 4, 2018 via iPhone
    1
    因为 sort 是原地排序,不返回任何东西…… help(a.sort)就知道了
    IanPeverell
    IanPeverell
    Apr 4, 2018
    2
    按照你的想法,应该写成

    b = sorted(a)

    a.sort() 是对 a 排序,无返回
    ballshapesdsd
    ballshapesdsd
    Apr 4, 2018
    3
    骚年要学会查 python 文档啊
    wlwood
    wlwood
    Apr 4, 2018 via Android
    4
    其实你写 C 排序的时候就明白了呀。传递一个指针数组过去,直接对那个内存地址进行操作,而不是重新分配地址来操作。这样就不用返回的结果了。
    congeec
    congeec
    Apr 4, 2018 via iPhone
    5
    函数式和命令式混用的语言,要小心
    qsnow6
    qsnow6
    Apr 4, 2018
    6
    a.sort()是个方法,无返回值;
    python 在执行无返回值的函数时,默认返回一个 None
    vegito2002
    vegito2002
    Apr 4, 2018 via iPad
    7
    @congeec 同感, 刚开始用的时候真的是各种踩坑。
    arischow
    arischow
    Apr 5, 2018 via iPhone❤️1
    8
    RTFD
    jinfeng333
    jinfeng333
    Apr 5, 2018
    9
    RTFM
    YangJian1994
    YangJian1994
    OP
    Apr 9, 2018
    10
    @ulala 好的
    YangJian1994
    YangJian1994
    OP
    Apr 9, 2018
    11
    @IanPeverell 谢谢,知道了
    YangJian1994
    YangJian1994
    OP
    Apr 9, 2018
    12
    @ballshapesdsd ok,会的
    YangJian1994
    YangJian1994
    OP
    Apr 9, 2018
    13
    @wlwood 受教了,谢谢
    YangJian1994
    YangJian1994
    OP
    Apr 9, 2018
    14
    @congeec 恩恩,知道了
    YangJian1994
    YangJian1994
    OP
    Apr 9, 2018
    15
    @qsnow6 恩恩,谢谢
    YangJian1994
    YangJian1994
    OP
    Apr 9, 2018
    16
    @vegito2002 是的
    YangJian1994
    YangJian1994
    OP
    Apr 9, 2018
    17
    @wujunze 会的
    YangJian1994
    YangJian1994
    OP
    Apr 9, 2018
    18
    @arischow RTFM 楼下正解
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4283 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 07:42 · PVG 15:42 · LAX 23:42 · JFK 02:42
    ♥ Do have faith in what you're doing.