为什么“返回list.Sort()”不返回列表,而不是返回列表?我已经证实findUniqueWords是否会导致排序list..然而,它没有return这个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
3 回答
长风秋雁
TA贡献1757条经验 获得超7个赞
answer = newList.sort()
sort
answer = sorted(newList)
慕容708150
TA贡献1831条经验 获得超4个赞
self
这来自于一种编码风格(我相信在其他各种语言中都很流行,特别是Lisp非常喜欢它),在这种风格中,对单个对象的一系列副作用可以这样链接起来: x.compress().chop(y).sort(z)
这和 x.compress() x.chop(y) x.sort(z)
我发现链接构成了对可读性的威胁;它要求读者必须熟悉每一种方法。第二种形式清楚地表明,每个调用都作用于同一个对象,因此即使您不太了解该类及其方法,也可以理解第二次和第三次调用都应用于x(所有调用都是针对它们的副作用进行的),而不是其他调用。
我想为返回新值的操作保留链接,比如字符串处理操作: y = x.rstrip("\n").split(":").lower()
添加回答
举报
0/150
提交
取消
