Why does “return list.sort()” return None, not the list?

前端 未结 7 724
感情败类
感情败类 2020-11-21 06:55

I\'ve been able to verify that the findUniqueWords does result in a sorted list. However, it does not return the list. Why?

def fin         


        
7条回答
  •  遇见更好的自我
    2020-11-21 07:36

    you can use sorted() method if you want it to return the sorted list. It's more convenient.

    l1 = []
    n = int(input())
    
    for i in range(n):
      user = int(input())
      l1.append(user)
    sorted(l1,reverse=True)
    

    list.sort() method modifies the list in-place and returns None.

    if you still want to use sort you can do this.

    l1 = []
    n = int(input())
    
    for i in range(n):
      user = int(input())
      l1.append(user)
    l1.sort(reverse=True)
    print(l1)
    

提交回复
热议问题