我已经能够验证findUniqueWords确实会导致一个排序的列表。但是,它不返回列表。为什么?

def findUniqueWords(theList):
    newList = []
    words = []

    # Read a line at a time
    for item in theList:

        # Remove any punctuation from the line
        cleaned = cleanUp(item)

        # Split the line into separate words
        words = cleaned.split()

        # Evaluate each word
        for word in words:

            # Count each unique word
            if word not in newList:
                newList.append(word)

    answer = newList.sort()
    return answer

当前回答

下面是Guido van Rossum在Python开发列表中的一封电子邮件,解释了为什么他选择在影响对象的操作上不返回self,并且不返回新操作。

This comes from a coding style (popular in various other languages, I believe especially Lisp revels in it) where a series of side effects on a single object can be chained like this: x.compress().chop(y).sort(z) which would be the same as x.compress() x.chop(y) x.sort(z) I find the chaining form a threat to readability; it requires that the reader must be intimately familiar with each of the methods. The second form makes it clear that each of these calls acts on the same object, and so even if you don't know the class and its methods very well, you can understand that the second and third call are applied to x (and that all calls are made for their side-effects), and not to something else. I'd like to reserve chaining for operations that return new values, like string processing operations: y = x.rstrip("\n").split(":").lower()

其他回答

Python有两种类型的排序:排序方法(或“成员函数”)和排序函数。sort方法对指定对象的内容进行操作——可以将其视为对象对自身进行重新排序的操作。sort函数是对由对象表示的数据进行的操作,并返回一个新对象,该对象具有按顺序排序的相同内容。

给定一个名为l的整数列表,如果调用l.sort(),列表本身将被重新排序:

>>> l = [1, 5, 2341, 467, 213, 123]
>>> l.sort()
>>> l
[1, 5, 123, 213, 467, 2341]

此方法没有返回值。但是如果我们尝试分配l.sort()的结果呢?

>>> l = [1, 5, 2341, 467, 213, 123]
>>> r = l.sort()
>>> print(r)
None

现在R实际上等于零。这是一个奇怪的,有点烦人的细节,程序员在离开Python一段时间后很可能会忘记(这就是我写这篇文章的原因,这样我就不会再忘记了)。

另一方面,函数sorted()不会对l的内容做任何操作,而是返回一个与l内容相同的新的排序列表:

>>> l = [1, 5, 2341, 467, 213, 123]
>>> r = sorted(l)
>>> l
[1, 5, 2341, 467, 213, 123]
>>> r
[1, 5, 123, 213, 467, 2341]

注意,返回值不是深度副本,所以要谨慎对列表中包含的元素进行副作用操作:

>>> spam = [8, 2, 4, 7]
>>> eggs = [3, 1, 4, 5]
>>> l = [spam, eggs]
>>> r = sorted(l)
>>> l
[[8, 2, 4, 7], [3, 1, 4, 5]]
>>> r
[[3, 1, 4, 5], [8, 2, 4, 7]]
>>> spam.sort()
>>> eggs.sort()
>>> l
[[2, 4, 7, 8], [1, 3, 4, 5]]
>>> r
[[1, 3, 4, 5], [2, 4, 7, 8]]

列表。Sort对列表进行排序,即它不会返回一个新的列表。只写

newList.sort()
return newList

下面是Guido van Rossum在Python开发列表中的一封电子邮件,解释了为什么他选择在影响对象的操作上不返回self,并且不返回新操作。

This comes from a coding style (popular in various other languages, I believe especially Lisp revels in it) where a series of side effects on a single object can be chained like this: x.compress().chop(y).sort(z) which would be the same as x.compress() x.chop(y) x.sort(z) I find the chaining form a threat to readability; it requires that the reader must be intimately familiar with each of the methods. The second form makes it clear that each of these calls acts on the same object, and so even if you don't know the class and its methods very well, you can understand that the second and third call are applied to x (and that all calls are made for their side-effects), and not to something else. I'd like to reserve chaining for operations that return new values, like string processing operations: y = x.rstrip("\n").split(":").lower()

问题就在这里:

answer = newList.sort()

Sort不会返回已排序的列表;相反,它对列表进行了适当的排序。

Use:

answer = sorted(newList)

如果想让它返回排序后的列表,可以使用sorted()方法。 这样更方便。

l1 = []
n = int(input())

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

list.sort()方法就地修改列表并返回None。

如果你还想使用排序,你可以这样做。

l1 = []
n = int(input())

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