Order dictionary and display 20 first

Asked

Viewed 108 times

0

I have a dictionary with words and their frequency. Of the kind:

dic = {walt: 2, disney: 1, ola: 5, ...}

To sort the dictionary I did so:

aux = sorted(dicionario, key=dicionario.get, reverse=True)

And to present the first 20 I wanted to do so, but not giving.

while count < 20:
    maior.append(dicionario.keys()[count])
    count +=1
return maior

2 answers

3

If you used the collections.Counter, as suggested in Sort dictionary and add python values, I believe you have not bothered to read the documentation. Before using whichever function is essential that you read the documentation and understand exactly what she does.

The class Counter has the method most_common which, as the name suggests, returns the most common items of the object.

counter = collections.Counter(palavras)
mais_comuns = counter.most_common(20)

1


Walt057, I think I have a solution to your code.

1. When we are defining a dictionary (which is a key data structure: value), we can use several types of variables, be they: str; int; float.

Thus, I constituted a dictionary for didactic purposes with some random words:

dictionary = {
    'walt': 2, 'disney': 1, 'ola': 5, 'teste': 12, 'you': 9, 'hasd': 6, 'asd1': 7, '91uas': 8, 'h91k': 9, '9ahsdkj': 10,
    'mais': 1, 'uma': 21, 'tchau': 8, 'mãe': 8, 'você': 6, 'computador': 12, 'mouse': 19, 'celular': 29, 'energetico': 9, 'caneta': 10,
    'vigesimo': 20, 'vigesimo primeiro': 20
}

Note that I put 22 items, if you want to check the size, we can use the Len function():

print(len(dictionary))

This will return the size of the dictionary.

2. To sort, we can use a simplified code:

dictionary_sorted = sorted(dictionary)

This will cause the respective dictionary to be sorted alphabetically. We use the key to get a list of the keys in the dictionary, and use get to get the specific value of a key.

If you want to check the size of the dictionary, it will not remove or add items and will continue to the same size as the primary dictionary.

print(len(dictionary_sorted))

The same will still have the 22 items.

3. As you want to display the first 20 values of the dictionary (sorted using the key in alphabetical order - note that the result will be an array, since dictionaries are not sorted), you can 'slice' the desired:

to_present = dictionary_sorted[:20]

':' is an indicator to show the respective positions we want. In case :20 (note that you will have nothing before :, and showing how far you want it). And in response, he will present a list with the words.

4. if you want to show in a simplified way, without having to perform a loop loop, you can use the Join function():

print(', '.join(to_present))

As an answer we will have all values of to_present (which we slice the first 20)

91uas, 9ahsdkj, asd1, caneta, celular, computador, disney, energetico, h91k, hasd, mais, mouse, mãe, ola, tchau, teste, uma, vigesimo,vigesimo primeiro, você

I hope I’ve cooperated with your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.