如何打印频率高于阈值的大写字符数(在教程中)?在
作业问题是:
Your task is to write a function which takes as input a single non-negative number and returns (not print) the number of characters in the tally whose count is strictly greater than the argument of the function. Your function should be called freq_threshold.
我的回答是:
mobyDick = "Blah blah A B C A RE."
def freq_threshold(threshold):
tally = {}
for char in mobyDick:
if char in tally:
tally[char] += 1
else:
tally[char] = 1
for key in tally.keys():
if key.isupper():
print tally[key],tally.keys
if threshold>tally[key]:return threshold
else:return tally[key]
这不管用,但我不知道哪里错了。在
不要重新发明轮子,而是使用counter object,例如:
你把每个字符的数字加起来的部分很好:
错误在于你是如何总结计票结果的。在
相反,您需要逐步检查字符计数和字符计数,count有多少字符的频率超过n。在
您的任务是返回满足条件的字符数。您试图返回某个字符的出现次数。试试这个:
你可以让这段代码更像Python。我这样写是为了让它更清楚。在
相关问题 更多 >
编程相关推荐