Python: Return 字符串中恰好出现一次的单词

Python: Return the words in a string that occur exactly once

假设我有一个接受一些字符串的函数,然后我需要 return 该字符串中恰好出现一次的单词集。执行此操作的最佳方法是什么?使用 dict 会有帮助吗?我尝试了一些伪代码,例如:

counter = {}
def FindWords(string):
    for word in string.split()
        if (word is unique): counter.append(word)
return counter

有没有更好的实现方式?谢谢!

编辑:

假设我有:"The boy jumped over the other boy"。我想 return "jumped," "over," 和 "other."

此外,我想return将其作为一个集合,而不是一个列表。

您可以使用 collections 中的 Counter 和 return 一组只出现一次的单词。

from collections import Counter

sent = 'this is my sentence string this is also my test string'

def find_single_words(s):
    c = Counter(s.split(' '))
    return set(k for k,v in c.items() if v==1)

find_single_words(sent)
# returns:
{'also', 'sentence', 'test'}

要仅使用基础 Python 实用程序执行此操作,您可以使用字典来记录出现次数,复制 Counter.

的功能
sent = 'this is my sentence string this is also my test string'

def find_single_words(s):
    c = {}
    for word in s.split(' '):
        if not word in c:
             c[word] = 1
        else:
             c[word] = c[word] + 1
    return [k for k,v in c.items() if v==1]

find_single_words(sent)
# returns:
['sentence', 'also', 'test']

这可能就是您的想法。

>>> counts = {}
>>> sentence =  "The boy jumped over the other boy"
>>> for word in sentence.lower().split():
...     if word in counts:
...         counts[word]+=1
...     else:
...         counts[word]=1
...         
>>> [word for word in counts if counts[word]==1]
['other', 'jumped', 'over']
>>> set([word for word in counts if counts[word]==1])
{'other', 'jumped', 'over'}

但是正如其他人所建议的那样,使用 Collections 中的 defaultdict 会更好。

s='The boy jumped over the other boy'
def func(s):
    l=[]
    s=s.split(' ')  #edit for case-sensitivity here
    for i in range(len(s)):
        if s[i] not in s[i+1:] and s[i] not in s[i-1::-1]:
            l.append(s[i])
    return set(l)  #convert to set and return
print(func(s))

这应该工作得很好。

检查每个元素是否有任何元素在其前面或后面的列表中与它匹配,如果没有则追加它。

如果您不想区分大小写,那么您可以在拆分之前添加 s=s.lower()s=s.upper()

你可以试试这个:

s = "The boy jumped over the other boy"
s1 = {"jumped", "over", "other"}
final_counts = [s.count(i) for i in s1]

输出:

[1, 1, 1]

试试这个。

>>> sentence = "The boy jumped over the other boy"
>>> set(word for word in sentence.lower().split() if sentence.count(word) == 1)
{'other', 'over', 'jumped'}
>>> 

编辑:这样更容易阅读:

>>> sentence = 'The boy jumped over the other boy'
>>> words = sentence.lower().split()
>>> uniques = {word for word in words if words.count(word) == 1}
>>> uniques
{'over', 'other', 'jumped'}
>>> type(uniques)
<class 'set'>