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

时间:2017-10-03 22:16:58

标签: python string set find-occurrences

假设我有一个函数接受一些字符串,然后我需要返回此字符串中恰好出现一次的单词集。这样做的最佳方法是什么?使用dict会有帮助吗?我尝试过一些伪代码:

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

有没有更好的方法来实现这个?谢谢!

编辑:

说我有:“男孩跳过另一个男孩”。我想返回“跳跃”,“结束”和“其他”。

另外,我想把它作为一个集合返回,而不是列表。

5 个答案:

答案 0 :(得分:3)

您可以使用Counter中的collections并返回一组仅出现一次的字词。

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']

答案 1 :(得分:0)

这可能就是你的想法。

>>> 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更好。

答案 2 :(得分:0)

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()

答案 3 :(得分:0)

你可以试试这个:

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

输出:

[1, 1, 1]

答案 4 :(得分:0)

试试这个。

>>> 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'>