字典以单词作为键,频率作为值,并在字典中找到具有相同字母长度的最高值

时间:2016-10-10 08:44:30

标签: python-3.x sorting dictionary

该函数打印关键字长度,然后打印所需长度的所有字典关键字的排序列表,其中频率最高,频率最高。例如,以下代码:

word_frequencies = {"and":15,  "whale":7,  "frog":7,  "cat":15,  "fish":1,  "dog":2,  "pig":15,  "stork":1,  "rat":15,  "bird":7}
print_most_frequent(word_frequencies, 3)
print_most_frequent(word_frequencies, 4)
print_most_frequent(word_frequencies, 5)

打印出来:

3 letter keywords: ['and', 'cat', 'pig', 'rat'] 15
4 letter keywords: ['bird', 'frog'] 7
5 letter keywords: ['whale'] 7

这是我到目前为止的尝试:

def print_most_frequent(words_dict, word_len):
    keyword_length = []
    frequency = 0
    for key in words_dict:
        if len(key) == word_len and words_dict[key] >= frequency:
            keyword_length += [key]
            if frequency != words_dict[key]:
                frequency += words_dict[key]
    print (str(word_len) + " letter keywords: " + str(keyword_length) + " " + str(frequency))

但是这给了我:

3 letter keywords: ['dog', 'cat'] 17
4 letter keywords: ['frog', 'bird'] 15
5 letter keywords: ['whale'] 7

有人能指出我正确的方向,所以我知道该怎么做。

我需要按字母顺序对字典进行排序,以便它出现:

['and', 'cat', 'pig', 'rat']

而不是随机化。

2 个答案:

答案 0 :(得分:1)

您的frequency变量可能意味着保留maximum frequency。不过,你对此的逻辑是错误的。如果您合并>=!=,它会为您提供:

  • max_frequency设为0
  • 如果下一个单词frequency高于此值,请将其frequency添加到max_frequency
  • 重复所有单词的最后一步

我认为你真正想要的是:

  • max_frequency设为0
  • 如果下一个单词' frequency高于,则设置 max_frequency到该单词' s frequency
  • 重复所有单词的最后一步
  • 查找具有该频率的所有字词

此外,您只需在列表中添加频率等于或高于frequency的字词。但我认为你想要添加具有正确字符数的所有单词。

长话短说:

    # filter words based on key length
    right_length = {k: v for k, v in words_dict.items() if len(k) == word_len}
    # calculate maximal value (max frequency)
    max_freq = max(right_length.values())
    # filter words with that max frequency
    max_words = {k: v for k, v, in right_length.items if v == max_freq}
    # now max_words contains all words of specified length with the maximum frequency
    # print words alphabetically
    print(sorted(max_words.keys()))

相同的代码更容易:

    # filter words based on key length
    right_length = {}
    for k, v in words_dict.items():
        if len(k) == word_len:
            right_length[k] = v
    # calculate maximal value (max frequency)
    max_freq = max(right_length.values())
    # filter words with that max frequency
    max_words = {}
    for k, v in right_length.items():
        if v == max_freq:
            max_words[k] = v
    # now max_words contains all words of specified length with the maximum frequency
    # print words alphabetically
    print(sorted(max_words.keys()))

答案 1 :(得分:1)

您可以使用dict comprehension创建一个字典,其中的键只有len等于word_len。然后,您可以使用max获取这些项目的最大值。最后,您可以创建一个列表,其中只包含频率等于最大频率的项目。

您可以使用sorted按字母顺序对列表进行排序。

word_frequencies = {"and":15,  "whale":7,  "frog":7,  "cat":15,  "fish":1,  "dog":2,  "pig":15,  "stork":1,  "rat":15,  "bird":7}

def print_most_frequent(words_dict, word_len):

    keys = words_dict.keys()
    max_length = max([len(i) for i in keys])  # Find max length of words
    min_length = min([len(i) for i in keys])  # Find min length of words

    if not min_length <= word_len <= max_length:
        print("{} letter keywords: [] -".format(word_len))
        return  # Exit function

    words = {i:j for i,j in words_dict.items() if len(i) == word_len}  # Create a dictionary containing words that are the same length as word_len
    max_value = max(words.values())  # Find the max frequency of the words in the above dictionary
    words_final = sorted([i for i,j in words.items() if j == max_value])  # Create a list of words that have the value of max_value 

    print("{} letter keywords: {} {}".format(word_len, words_final, max_value))

输出:

>>> print_most_frequent(word_frequencies,3)
3 letter keywords: ['and', 'cat', 'pig', 'rat'] 15
>>> print_most_frequent(word_frequencies,4)
4 letter keywords: ['bird', 'frog'] 7
>>> print_most_frequent(word_frequencies,5)
5 letter keywords: ['whale'] 7
>>> print_most_frequent(word_frequencies,10)
10 letter keywords: [] -

您可能想稍微清理一下代码。但它应该足以让你开始。