Inverting a dictionary when some initial values ​​are identical

Let's say I have a dictionary called word_counter_dictionarythat counts how many words a document contains in a form {'word' : number}. For example, the word "secondly" appears once, so there will be a key / value pair {'secondly' : 1}. I want to make an inverted list so that numbers become keys, and words become values ​​for these keys, so I can then display the 25 most popular words. I saw somewhere where the function setdefault()can come in handy, but no matter what I can not use it, because so far in the class in which I participate, we have considered only get().

inverted_dictionary = {}
for key in word_counter_dictionary:
    new_key = word_counter_dictionary[key]
    inverted_dictionary[new_key] = word_counter_dictionary.get(new_key, '') + str(key)   
    inverted_dictionary

Until now, using this method above, it works fine until it reaches another word with the same meaning. For example, a word "saves"also appears once in a document, so Python will add a new key / value pair just fine. BUT it erases {1 : 'secondly'}with a new pair, so it’s only {1 : 'saves'}in the dictionary.

So, in the end, my goal is to get ALL the words and their number of repetitions in this new dictionary called inverted_dictionary.

+4
source share
5 answers

A defaultdict is perfect for this

word_counter_dictionary = {'first':1, 'second':2, 'third':3, 'fourth':2}
from collections import defaultdict

d = defaultdict(list)
for key, value in word_counter_dictionary.iteritems():
    d[value].append(key)

print(d)

Output:

defaultdict(<type 'list'>, {1: ['first'], 2: ['second', 'fourth'], 3: ['third']})
+4
source

What you can do is convert the value to a list of words with the same key:

word_counter_dictionary = {'first':1, 'second':2, 'third':3, 'fourth':2}

inverted_dictionary = {}
for key in word_counter_dictionary:
    new_key = word_counter_dictionary[key]
    if new_key in inverted_dictionary:
        inverted_dictionary[new_key].append(str(key))
    else:
        inverted_dictionary[new_key] = [str(key)]

print inverted_dictionary

>>> {1: ['first'], 2: ['second', 'fourth'], 3: ['third']}
+1
source

Python dicts , (1 ). list , , :

inverted_dictionary = {}
for key in word_counter_dictionary:
    new_key = word_counter_dictionary[key]
    if new_key in inverted_dictionary:
        inverted_dictionary[new_key].append(key)
    else:
        inverted_dictionary[new_key] = [key]

25 , () inverted_dictionary :

common_words = []
for key in sorted(inverted_dictionary.keys(), reverse=True):
    if len(common_words) < 25:
        common_words.extend(inverted_dictionary[key])
    else: 
        break

common_words = common_words[:25] # In case there are more than 25 words
0

, "" :

>>> import operator
>>> A = {'a':10, 'b':843, 'c': 39, 'd': 10}
>>> B = sorted(A.iteritems(), key=operator.itemgetter(1), reverse=True)
>>> B
[('b', 843), ('c', 39), ('a', 10), ('d', 10)]

, , , .

25, : B[:25].

( ):

>>> [x[0] for x in B]
['b', 'c', 'a', 'd']
>>> [x[1] for x in B]
[843, 39, 10, 10]

>>> C, D = zip(*B)
>>> C
('b', 'c', 'a', 'd')
>>> D
(843, 39, 10, 10)

, ( ), . , .

0

.

(, , ):

word_counter_dictionary = {'first':1, 'second':2, 'third':3, 'fourth':2}
counter_word_list = sorted((count, word) for word, count in word_counter_dictionary.items())

:

>>> print(counter_word_list[-2:])
[(2, 'second'), (3, 'third')]

Python (heapq.nlargest ):

import heapq, operator
print(heapq.nlargest(2, word_counter_dictionary.items(), key=operator.itemgetter(1)))

:

[('third', 3), ('second', 2)]
0

All Articles