小编典典

为什么“return list.sort()”返回None,而不是列表?

all

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

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

阅读 105

收藏
2022-05-23

共1个答案

小编典典

list.sort对列表进行就地排序,即它不返回新列表。写吧

newList.sort()
return newList
2022-05-23