【发布时间】:2017-10-03 22:16:57
【问题描述】:
假设我有一个接收某个字符串的函数,然后我需要返回该字符串中恰好出现一次的单词集。这样做的最佳方法是什么?使用 dict 会有帮助吗?我尝试了一些伪代码,例如:
counter = {}
def FindWords(string):
for word in string.split()
if (word is unique): counter.append(word)
return counter
有没有更好的方法来实现这一点?谢谢!
编辑:
假设我有:“那个男孩跳过了另一个男孩”。我想返回“jumped”、“over”和“other”。
另外,我想把它作为一个集合返回,而不是一个列表。
【问题讨论】:
-
你有哪组词?
-
假设我有一组词,例如:“那个男孩跳过了另一个男孩”。我想返回“jumped”、“over”和“other”。
标签: python string set find-occurrences