Python:以字符串形式返回恰好出现一次的单词
假设我有一个函数,它接收一些字符串,然后我需要返回这个字符串中恰好出现一次的单词集。做这件事最好的方法是什么?使用dict会有帮助吗?我试过一些伪代码,比如:Python:以字符串形式返回恰好出现一次的单词,python,string,set,find-occurrences,Python,String,Set,Find Occurrences,假设我有一个函数,它接收一些字符串,然后我需要返回这个字符串中恰好出现一次的单词集。做这件事最好的方法是什么?使用dict会有帮助吗?我试过一些伪代码,比如: counter = {} def FindWords(string): for word in string.split() if (word is unique): counter.append(word) return counter 有没有更好的方法来实现这一点?谢谢 编辑: 我说过:“那个男孩跳过了另一个
counter = {}
def FindWords(string):
for word in string.split()
if (word is unique): counter.append(word)
return counter
有没有更好的方法来实现这一点?谢谢
编辑:
我说过:“那个男孩跳过了另一个男孩”。我想返回“跳过”、“跳过”和“其他”
另外,我想将其作为一个集合而不是列表返回。您可以使用
集合中的计数器
,返回一组只出现一次的单词
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实用程序实现这一点,可以使用字典来记录出现的次数,复制计数器的功能
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'}
但正如其他人所建议的,使用集合中的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'>
>句子='男孩跳过了另一个男孩'
>>>单词=句子。lower()。split()
>>>uniques={words for words in words if words.count(word)==1}
>>>独特的
{'over','other','jumped'}
>>>类型(uniques)
你有哪一组词?假设我有一组词,比如:“那个男孩跳过了另一个男孩”。我想返回“跳过”、“跳过”和“其他”。有没有一种方法可以在不导出计数器等外部工具的情况下执行此操作?@J.P.集合
是标准库的一部分,它实际上不是外部工具tool@J.P.我在我的答案中增加了一部分,见上文嗨,谢谢!你知道如果你想返回一个集合而不是一个列表,你会如何改变它吗?你能返回一个集合吗?@J.P.当然,我修改了答案的第二部分以返回一个集合,而不是c.items()。它应该只返回“跳过”、“跳过”和“其他”。谢谢!您知道如何将其作为集合而不是列表返回吗?在中添加了该选项。set()将一个列表更改为一个集合。遍历每个单词的整个单词列表使其成为一个O(n^2)算法,当输入变大时,该算法会变得非常慢。使用字典计算出现的次数可以更好地扩展到大型输入。