如何根据 python 中的键值计算字典的频率?
How to compute the frequencies of a dictionary based on the key values in python?
假设我有一个如下形式的字典,由单词和短语组成。
{
('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]
}
我想根据给定的键值计算所有 words/phrases 的 frequency
。
例如:只有 home
的频率需要为 2(因为它同时出现在 3 和 4 键值中)。所有 words/phrases 的休息频率都是 1.
我试过使用
Counter(index.values()).most_common()
在 python 中是否有任何方法可以做到这一点?
米什拉。你可以试试
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])
您可以使用字典理解来获取以短语作为键并以计数作为值的字典。
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}
假设我有一个如下形式的字典,由单词和短语组成。
{
('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]
}
我想根据给定的键值计算所有 words/phrases 的 frequency
。
例如:只有 home
的频率需要为 2(因为它同时出现在 3 和 4 键值中)。所有 words/phrases 的休息频率都是 1.
我试过使用
Counter(index.values()).most_common()
在 python 中是否有任何方法可以做到这一点?
米什拉。你可以试试
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])
您可以使用字典理解来获取以短语作为键并以计数作为值的字典。
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}