Python 3.x 函数返回重复的单词(如果重复)>;=n次
我正在创建一个函数,如果单词重复一定次数(n),则返回单词Python 3.x 函数返回重复的单词(如果重复)>;=n次,python-3.x,Python 3.x,我正在创建一个函数,如果单词重复一定次数(n),则返回单词 def repeat_word_count(文本,n): 计数={} 对于text.split()中的文本: 如果文本输入计数: 计数[文本]+=1 其他: 计数[文本]=1 返回[如果counts.values()>=int(n),则计数中计数的计数] 打印(重复单词计数(“水牛”,2)) 上面是执行时返回属性错误的函数 Traceback (most recent call last): File "program.py
def repeat_word_count(文本,n):
计数={}
对于text.split()中的文本:
如果文本输入计数:
计数[文本]+=1
其他:
计数[文本]=1
返回[如果counts.values()>=int(n),则计数中计数的计数]
打印(重复单词计数(“水牛”,2))
上面是执行时返回属性错误的函数
Traceback (most recent call last):
File "program.py", line 10, in <module>
print(repeat_word_count("one one was a racehorse two two was one too", 3))
File "program.py", line 9, in repeat_word_count
return [counts for counts in counts if counts.values() >= int(n)]
File "program.py", line 9, in <listcomp>
return [counts for counts in counts if counts.values() >= int(n)]
AttributeError: 'str' object has no attribute 'values'
回溯(最近一次呼叫最后一次):
文件“program.py”,第10行,在
打印(重复单词计数(“一个是赛马,两个也是一”,3))
文件“program.py”,第9行,重复单词计数
返回[如果counts.values()>=int(n),则计数中计数的计数]
文件“program.py”,第9行,在
返回[如果counts.values()>=int(n),则计数中计数的计数]
AttributeError:“str”对象没有属性“值”
我该如何解决这个问题呢?我认为您的代码的一个问题是在
counts
集合的迭代中重用counts
变量
from collections import Counter
def repeat_word_count(text, n):
counts = Counter(text.split(' '))
return [word for word in counts if counts[word] >= int(n)]
print(repeat_word_count("buffalo buffalo test test2 test2 buffalo buffalo", 2))
修复后,下一个错误是将集合counts.values()
与intn
进行比较
我认为您可以利用计数器
集合获得更好、更简单的解决方案
from collections import Counter
def repeat_word_count(text, n):
counts = Counter(text.split(' '))
return [word for word in counts if counts[word] >= int(n)]
print(repeat_word_count("buffalo buffalo test test2 test2 buffalo buffalo", 2))
问题在于列表理解行:
return [counts for counts in counts if counts.values() >= int(n)]
您正在重新分配计数。列表中的counts.values()
位置也不正确
只要修改代码,它应该是这样的:
return [count for count in counts.values() if count >= int(n)]
然而,这只是返回计数,而不是文字。如果你想要这些词,你需要这样的词:
return [word for word, count in counts.items() if count >= int(n)]
def repeat_word_count(text, n):
counts ={}
for text in text.split():
try:
counts[text] += 1
except KeyError:
counts[text] = 1
return [word for word, count in counts.items() if count >= int(n)]
print(repeat_word_count("buffalo buffalo buffalo buffalo", 2))
就我个人而言,我处理核对和计数的方式也会有所不同。有些人建议使用一种稍微“pythonic”的方式来处理dict条目的检查并增加它们,即使用try/except,如下所示:
return [word for word, count in counts.items() if count >= int(n)]
def repeat_word_count(text, n):
counts ={}
for text in text.split():
try:
counts[text] += 1
except KeyError:
counts[text] = 1
return [word for word, count in counts.items() if count >= int(n)]
print(repeat_word_count("buffalo buffalo buffalo buffalo", 2))
这背后的原因是,如果你在dict中找到它,你只需要查找一次,然后增加它。这不是在dict中查看它是否存在,然后在dict中再次查找它以增加条目。使用try/except方法,当它在字典中找不到它时,它会执行keyrerror异常,并将初始值为1的条目添加到dict中。您可以使用Collections Counter函数执行此操作:
from collections import Counter
def repeat_word_count(text, n):
counter = Counter(text.split())
return [{k: counter for k, counter in counter.items() if counter >= n}]
print(repeat_word_count("buffalo buffalo buffalo buffalo", 2)) #[{'buffalo': 4}]
print(repeat_word_count("buffalo buffalo buffalo buffalo", 5)) #[{}]