為什么“返回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
提交
取消