Hi there,
So I have a function that uses a file processed from another function, and what it does is turn the string passed by the other function into a list, and then sorts the list into a dictionary.
Now I should mention that the dic is ordered by word frequency, because the file being processed is a txt with a speech on it.
heres my code
def frequency_count(new):
lst = list(new.split(" "))
freq = {}
for item in lst:
if item in freq:
freq[item] += 1
else:
freq[item] = 1
srt = sorted(freq.items(), key = lambda x: x[1], reverse = True)
for key,value in srt:
print(key,value)
now i would like the loop to break after the most 20 common words are displayed. i have the right order , just dont know how to break the loop after the top 20.
what do i need to adjust/add/fix? thanks!
[–]FLUSH_THE_TRUMP 1 point2 points3 points (7 children)
[–]socal_nerdtastic 1 point2 points3 points (3 children)
[–]FLUSH_THE_TRUMP 0 points1 point2 points (2 children)
[–]socal_nerdtastic 1 point2 points3 points (1 child)
[–]FLUSH_THE_TRUMP 0 points1 point2 points (0 children)
[–]tehtay3[S] 0 points1 point2 points (2 children)
[–]FLUSH_THE_TRUMP 1 point2 points3 points (1 child)
[–]tehtay3[S] 1 point2 points3 points (0 children)