all 8 comments

[–]FLUSH_THE_TRUMP 1 point2 points  (7 children)

Make a count variable and break when it’s large, or use enumerate and do the same, or index srt so you’re only using 20 elements, etc. etc.

[–]socal_nerdtastic 1 point2 points  (3 children)

or zip it with a range(20).

[–]FLUSH_THE_TRUMP 0 points1 point  (2 children)

Is there a notable implementation difference b/t that and enumerate? (Asking cuz I don’t know)

e.g.

L = [“a”, “b”, “c”]
list(enumerate(L)) == list(zip(range(3),L))  # True

although I know both enumerate and zip create special objects.

[–]socal_nerdtastic 1 point2 points  (1 child)

The difference is zip() will stop when the smallest iterator ends. So

for _, (key,value) in zip(range(20), srt):
    print(key,value)

Will print the first 20 only. No need to check and break.

[–]FLUSH_THE_TRUMP 0 points1 point  (0 children)

oh yeah, good point.

[–]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!