Stuck on the question "Write a function repeat_word_count(text, n) that takes a string text and a positive integer n, converts text into a list of words based on simple whitespace separation (with no removal of punctuation or changing of case), and returns a sorted list of words that occur n or more times in text. Eg repeat_word_count("wings wings wings wings", 2) -> output is ['wings'] So far I have got:
def repeat_word_count(text, n):
tally = {}
for char in repeat_word_count:
if char in tally:
tally[char] += 1
else:
tally[char] = 1
if tally[char] >= n :
print(tally.sorted())
I wanted to end with 'return tally' but I kept getting indent errors. But in general, I am getting the error TypeError: 'function' object is not iterable which I don't understand, if someone could explain that too it would help tons!
def repeat_word_count(text, n):
#split string by " " into a list of words
words = text.split(" ")
words_iterator={}
#interate throught the words
for word in words:
#if word is in the keys add 1 to his count
if word in words_iterator.keys():
words_iterator[word]+=1
#else add the word as a key and the counter as 1 giving is
#the first time the word appeard in the list
else:
words_iterator[word] = 1
output=[]
#parse the dict key by key
for key in words_iterator:
# if the value of the key is bigger than n add key to list
if words_iterator[key]>=n:
output.append(key)
return output
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.