简体   繁体   English

打印字典中的单词列表

[英]Printing list of words from dictionary

(Sorry for the long question) (很长的问题很抱歉)

print_most_common() function which is passed two parameters, a dictionary containing words and their corresponding frequencies eg print_most_common()函数传递两个参数,一个包含单词及其相应频率的字典,例如

{"fish":9,  "parrot":8,  "frog":9,  "cat":9,  "stork":1,  "dog":4, "bat":9,  "rat":3}

and, an integer, the required number of characters. 以及整数,要求的字符数。 The function gets a list of all the words of the required number of characters which are keys of the dictionary and have the highest frequency for words of that length. 该函数获取所需数量的字符的所有单词的列表,这些字符是字典的键,并且对于该长度的单词具有最高的频率。 The function first prints the string made up of the word length (the second parameter), followed by " letter keywords: ", then prints a list of all the words of the required length (keys from the dictionary) which have the highest frequency followed by the frequency value. 该函数首先打印由单词长度(第二个参数)组成的字符串,然后打印“字母关键字:”,然后打印具有所需长度的所有单词的列表(字典中的键),其频率最高通过频率值。 The list of words must be sorted alphabetically. 单词列表必须按字母顺序排序。

eg 例如

word_frequencies = {"fish":9, "parrot":8, "frog":9, "cat":9,
                                           "stork":1, "dog":4, "bat":9, "rat":3}
print_most_common(word_frequencies, 3)
print_most_common(word_frequencies, 4)
print_most_common(word_frequencies, 5)

Will print: 将打印:

3 letter keywords: ['bat', 'cat'] 9
4 letter keywords: ['fish', 'frog'] 9
5 letter keywords: ['stork'] 1

How would I define the print_most_common(words_dict, word_len) function? 我如何定义print_most_common(words_dict,word_len)函数?

how about this. 这个怎么样。

freq_dict = {k: v for k, v in word_frequencies.items() if len(k) == word_len}

for example: 例如:

>> freq_dict = {k: v for k, v in word_frequencies.items() if len(k) == 3}
>> print(freq_dict)
>> {'bat': 9, 'cat': 9, 'dog': 4, 'rat': 3}

This should work for a Python 2 implementation at least, updating to 3 should not be difficult. 这至少应该对Python 2实现有效,将其更新到3应该并不困难。

Asav provides a way to get dictionary with a words of word_len and their corresponding frequency. Asav提供了一种获取单词word_len及其对应频率的字典的方法。 You can then retrieve the max value from the frequencies and consequently retrieve the list of words with that frequency. 然后,您可以从频率中检索最大值,从而检索具有该频率的单词列表。

def print_most_common(words_dict, word_len):
    wl_dict = {k: v for k, v in words_dict.items() if len(k) == word_len}
    max_value = wl_dict[max(wl_dict, key=wl_dict.get)]
    res_list = [key for key,val in wl_dict.items() if val == max_value]
    print '%d letter keywords %s %d' % (word_len, res_list, max_value)

Do let me know if you want further decomposition or explanation. 如果您需要进一步的分解或解释,请告诉我。

Here is a possible solution: 这是一个可能的解决方案:

  1. Get all words of the required length. 获取所需长度的所有单词。

     filtered_words = {k: v for k, v in words_dict.items() if len(k) == word_len} 
  2. Get the maximum count for that length. 获取该长度的最大计数。

     max_count = max(filtered_words.values()) 
  3. Filter the words with that count. 用该计数过滤单词。

     [k for k, v in filtered_words.items() if v == max_count] 

Full Code 完整代码

def print_most_common(words_dict, word_len):
  filtered_words = {k: v for k, v in words_dict.items() if len(k) == word_len}
  max_count = max(filtered_words.values())
  print word_len, 'letter keywords:', [k for k, v in filtered_words.items() if v == max_count], max_count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM