Oulchura Oulchura - 6 months ago 19
Python Question

Python - My frequency function is inefficient

I'm writing a function that returns the number of times appeared of a word that appeared the most in the list of words.

def max_frequency(words):
"""Returns the number of times appeared of the word that
appeared the most in a list of words."""

words_set = set(words)
words_list = words
word_dict = {}

for i in words_set:
count = []
for j in words_list:
if i == j:
count.append(1)
word_dict[i] = len(count)

result_num = 0
for _, value in word_dict.items():
if value > result_num:
result_num = value
return result_num


For example:

words = ["Happy", "Happy", "Happy", "Duck", "Duck"]
answer = max_frequency(words)
print(answer)

3


But this function is slow when dealing with large amount of words in a list, for example, a list of 250,000 words takes over 4 minutes for this function to present the output. So I'm seeking help to tweak this.

I don't want to import anything.

Answer

While I fully agree with the comments related to your I don't want to import anything statement, I found your question amusing, so let's try it.

You don't need to build a set. Just go directly with words.

words = words = ["Happy", "Happy", "Happy", "Duck", "Duck"]
words_dict = {}

for w in words:
    if w in words_dict:
        words_dict[w] += 1
    else:
        words_dict[w] = 1

result_num = max(words_dict.values())

print(result_num)
# 3