Inverting a dictionary when some of the original values are identical

后端 未结 5 807
北海茫月
北海茫月 2021-01-19 23:57

Say I have a dictionary called word_counter_dictionary that counts how many words are in the document in the form {\'word\' : number}. For example,

5条回答
  •  攒了一身酷
    2021-01-20 00:53

    Python dicts do NOT allow repeated keys, so you can't use a simple dictionary to store multiple elements with the same key (1 in your case). For your example, I'd rather have a list as the value of your inverted dictionary, and store in that list the words that share the number of appearances, like:

    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]
    

    In order to get the 25 most repeated words, you should iterate through the (sorted) keys in the inverted_dictionary and store the words:

    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
    

提交回复
热议问题