我已经能够验证findUniqueWords
确实导致排序list
.然而,它没有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
Run Code Online (Sandbox Code Playgroud) 我对导致这种情况的思维过程感兴趣.对我来说,一个相对新手,似乎阻碍了,因为它阻止了列表处理的"链接"(例如mylist.reverse().append('a string')[:someLimit]
).我想可能是"那些人的力量"决定列表理解是一个更好的范例(一个有效的观点),所以不想鼓励其他方法 - 但是这似乎有悖常理,以防止一种直观的方法,即使更好存在替代品.
请注意,我不是在抱怨(我敢肯定有是一个合理的理由,我在它是什么只是感兴趣!),也不是寻找一个解决方案(意见在这里非常具有启发性) -只是寻找一些情况下,并深入理解语言的设计过程.