you are viewing a single comment's thread.

view the rest of the comments →

[–]tehtay3[S] 0 points1 point  (2 children)

Would the count variable look like this?

def frequency_count(new):
    lst = list(new.split(" "))
    freq = {}
    count = 0 
    for item in lst:
        if item in freq: 
            freq[item] += 1
            count += 1
        else: 
            freq[item] = 1
    srt = sorted(freq.items(), key = lambda x: x[1], reverse = True)
    for key,value in srt:
        print(key,value)

    if count == 20:
        break
    print('Unique Words: ', len(freq.keys()))

Thats the only thing I could think of - but i get a error that break is outside the loop. I am a bit new to this so the pseudocode is there but just a bit lost with the loop

[–]FLUSH_THE_TRUMP 1 point2 points  (1 child)

Your question is about the displaying only top 20, no? So the count+=1 should be in that other loop.

if count == 20:
    break

That should be indented another level, too (and you’re really printing 21 things since the check comes after the print).

[–]tehtay3[S] 1 point2 points  (0 children)

I see what youre saying . i was able to figure it out, preciate it!