Python Sort() method

后端 未结 4 478
感情败类
感情败类 2020-12-11 09:22

I am starting to learn Python.

Can someone explain why sort() returns None?

alist.sort()            ## correct
alist = blist.sort()    ## NO incorrec         


        
4条回答
  •  -上瘾入骨i
    2020-12-11 09:34

    alist.sort() sorts alist in-place, modifying alist itself.

    If you want a new list to assign somewhere, use blist = sorted(alist)

    • list.sort(): http://docs.python.org/library/stdtypes.html#mutable-sequence-types
    • sorted(): http://docs.python.org/library/functions.html#sorted

提交回复
热议问题