I have a piece of text and I have created a dictionary in python. It has words as keys, and the number of times the words has occurred in the text as values. This dictionary is sorted in decreasing value of the values field. Here is a snippet of my list:
[('the\n', 1644), ('and\n', 872), ('to\n', 729), ('a\n', 632), ('she\n', 541),
('it\n', 530), ('of\n', 514), ('said\n', 462), ('i\n', 410), ('alice\n', 386),
('in\n', 369), ('you\n', 365), ('was\n', 357), ('that\n', 280), ('as\n', 263),
('her\n', 248), ('at\n', 212), ('on\n', 193), ('all\n', 182), ('with\n', 181),
('had\n', 178), ('but\n', 170), ('for\n', 153), ('so\n', 151), ('be\n', 148),
('not\n', 145), ('very\n', 144), ('what\n', 136), ('this\n', 134),
('they\n', 130), ('little\n', 128), ('he\n', 120), ('out\n', 117),
('is\n', 108), ... ]
I want to print the 25 most frequent words. that is fairly simple and I have done it. The next part is to print the 25 most frequent words starting with the letter “f”. How do I find this and append it to the list of 25 most frequent words?
Also, I have to add a rank of all the words. For example, in my dictionary, “the” will be ranked 1, “and” 2 and so on. How do I add a rank to the list of words?
Just filter using a list comprehension:
Since the original list is sorted, so will this one be. You can then just slice it to get the top 25:
f_words[:25]