Как вычислить частоты словаря на основе значений ключа в Python? - PullRequest
3 голосов
/ 10 мая 2019

Предположим, у меня есть словарь в следующей форме, состоящий из слов и фраз.

{
    ('The brown fox',): [0], ('the race',): [0], ('Apple',): [1], 
    ('a company Apple',): [1], ('iphone',): [1], ('Paris',): [2],
    ('Delhi',): [2], ('London',): [2], ('world cities',): [2], 
    ('home',): [3, 4], ('order delivery food',): [3], ('simple voice command',): [3], 
    ('dinner',): [3], ('a long day',): [3], ('work',): [3], 
    ('teams',): [4], ('goal home',): [4], ('fox world',): [5], 
    ('a world class company',): [5], ('A geyser heating system',): [6], ('a lot',): [7], 
    ('the book Python',): [7], ('an amazing language',): [7], ('i',): [8], 
    ('a good boy',): [8], ('Team Performance',): [9], ('Revolv central automation device',): [10], 
    ('the switch way',): [11], ('play children',): [12]
}

Я хочу вычислить frequency всех слов / фраз на основе заданных значений ключа.

Например: только частота home должна быть равна 2 (как указано в значениях ключа 3 и 4). Частота отдыха всех слов / фраз равна 1.

Я пытался использовать

Счетчик (index.values ​​()). Most_common ()

Существует ли способ сделать это в python?

Ответы [ 2 ]

1 голос
/ 10 мая 2019

Вы могли бы использовать диктовку, чтобы получить диктат с фразами в качестве ключей и считается как значения.

d = {('The brown fox',): [0], ('the race',): [0], ('Apple',): [1], ('a company Apple',): [1], ('iphone',): [1], ('Paris',): [2], ('Delhi',): [2], ('London',): [2], ('world cities',): [2], ('home',): [3, 4], ('order delivery food',): [3], ('simple voice command',): [3], ('dinner',): [3], ('a long day',): [3], ('work',): [3], ('teams',): [4], ('goal home',): [4], ('fox world',): [5], ('a world class company',): [5], ('A geyser heating system',): [6], ('a lot',): [7], ('the book Python',): [7], ('an amazing language',): [7], ('i',): [8], ('a good boy',): [8], ('Team Performance',): [9], ('Revolv central automation device',): [10], ('the switch way',): [11], ('play children',): [12]}

frequency = {k[0]: len(v) for k, v in d.items()}

print(frequency)
# {'The brown fox': 1, 'the race': 1, 'Apple': 1, 'a company Apple': 1, 'iphone': 1, 'Paris': 1, 'Delhi': 1, 'London': 1, 'world cities': 1, 'home': 2, 'order delivery food': 1, 'simple voice command': 1, 'dinner': 1, 'a long day': 1, 'work': 1, 'teams': 1, 'goal home': 1, 'fox world': 1, 'a world class company': 1, 'A geyser heating system': 1, 'a lot': 1, 'the book Python': 1, 'an amazing language': 1, 'i': 1, 'a good boy': 1, 'Team Performance': 1, 'Revolv central automation device': 1, 'the switch way': 1, 'play children': 1}
1 голос
/ 10 мая 2019

Мишра.Вы можете попробовать

frequencies = []
for key in your_dictionary.keys():
    frequencies.append(len(your_dictionary[key]))

, если вы просто хотите, чтобы частоты были выделены в списке.

Или если вы хотите иметь возможность получить частоту из слова или фразы:

frequency_from_phrase = {}
for key in your_dictionary.keys():
    frequency_from_phrase[key] = len(your_dictionary[key])
...