Suppose that I have a dictionary in the following form, consisting of both words and phrases.
{
('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]
}
I want to compute the frequency
of all the words/phrases based on the given key values.
For example: Only the frequency of home
needs to be 2 (as it is appearing in both 3 and 4 key values). The frequency of rest all the words/phrases are 1.
I have tried using
Counter(index.values()).most_common()
Does there exist any way to do this in python?
You could use dict comprehension to get a dict with phrases as keys and counts as values.
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With