【问题标题】:Dictionary Issue Python字典问题 Python
【发布时间】:2016-02-05 08:45:16
【问题描述】:

问题:

定义print_most_common()函数,它传递两个参数,一个包含单词及其对应频率的字典,例如,

{"and":15,  "talon":7,  "frog":1,  "cat":15,  "tests":1,  "dog":2, "bat":14,  "rat":15}

和一个整数,字符数。

该函数获取所需字符数的所有单词的列表,这些单词是字典的键并且对于该长度的单词具有最高频率。

该函数首先打印由单词长度(第二个参数)组成的字符串,后跟“字母单词:”,然后打印字典中所有具有最高频率的所需长度单词的列表由频率值。例如以下代码:

word_frequencies =  {"and":15, "talon":7, "frog":1, "cat":15, "tests":1,  "dog":2, "bat":14, "rat":15}

print_most_common(word_frequencies, 3)

print_most_common(word_frequencies, 4)

print_most_common(word_frequencies, 5)

print_most_common(word_frequencies, 6)

打印以下三行:

3 letter words: ['and', 'cat', 'rat'] 15

4 letter words: ['frog'] 1

5 letter words: ['talon'] 7

6 letter words: [] 0

我尝试了这个问题,并提出了以下代码(我意识到这不是最好的方法,但我们还没有详细学习 python 编程):

def print_most_common(words_dict, word_len): 
    words_list = list(words_dict.keys())
    frequency = 0
    max_word_list = []
    for word in words_list:
        if word_len == len(word) and frequency <= words_dict[word]:
            frequency = words_dict[word]
    for word, value in words_dict.items():
        if value == frequency:
            max_word_list += [word]
    print (word_len, "letter words:", max_word_list, frequency)

我遇到的问题是代码在频率 3、5、6、0 下工作正常。但是,对于频率 4,它也打印出一个 5 个字母的单词:

预期结果:

3 letter words: ['and', 'cat', 'rat'] 15
4 letter words: ['frog'] 1
5 letter words: ['talon'] 7
6 letter words: [] 0

实际结果:

3 letter words: ['and', 'rat', 'cat'] 15
4 letter words: ['tests', 'frog'] 1
5 letter words: ['talon'] 7
6 letter words: [] 0

预期结果:

3 letter words: ['and', 'cat', 'pig', 'rat'] 15
4 letter words: ['bird', 'frog'] 7
5 letter words: ['front', 'tests'] 1

实际结果:

3 letter words: ['and', 'pig', 'rat', 'cat'] 15
4 letter words: ['talons', 'frog', 'bird'] 7
5 letter words: ['tests', 'front'] 1

我不知道我的代码哪里出错了。

【问题讨论】:

  • 请忽略。我意识到我错过了:'and word_len == len(word):' from the second for statement

标签: python


【解决方案1】:

计算出最大频率后,然后选择具有该频率的所有个单词,无论长度如何:

for word, value in words_dict.items():
    if value == frequency:
        max_word_list += [word]

您需要再次测试长度:

for word, value in words_dict.items():
    if len(word) == word_len and value == frequency:
        max_word_list += [word]

您可以一次性收集给定频率和字长的所有单词,但是:

def print_most_common(words_dict, word_len):
    results = []
    max_frequency = 0
    for word, freq in words_dict.items():
        if len(word) != word_len or freq < max_frequency:
            # wrong length or not frequent enough
            continue
        if freq == max_frequency:
            results.append(word)
        else:
            # right word length and *higher* frequency
            max_frequency = freq
            results = [word]
    print (word_len, "letter words:", results, max_frequency)

【讨论】:

  • 我尝试了两种方法(我的和你的建议),但它似乎不起作用......它给出了正确的答案,但根据 coderunner 它说它是不正确的。我不确定我错过了什么。截图link
  • @Nume:有趣;仅从该图像中,我会说您的输出与预期输出完全相同
  • @Nume:您的更正版本(在len(word) == word_len 测试中添加)和我的版本都会产生准确的输出,当然。
  • @Nume:啊,确实。那是……不公平,真的,因为任务描述肯定没有提到这个要求。
  • 我们所有的分配/任务似乎都是这样的。那好吧。非常感谢您的帮助。
【解决方案2】:

您需要检查字典循环中的单词长度,即and len(word) == word_len,频率可能匹配但单词可以是任何长度,因为您不检查:

for word, value in words_dict.items():
      # make sure the word_len is also equal
      if value == frequency and len(word) == word_len:
          max_word_list.append(word)

你也可以每次都简单地追加单词,不需要将单词包裹在列表中并扩展,for循环也可以做成推导式。

max_word_list = [word  for word, value in words_dict.items() if value == frequency and len(word) == word_len]

你也可以只取最大值:

def print_most_common(words_dict, word_len):
    mx = max(v for k, v in words_dict.items() if len(k) == word_len)
    max_word_list = [word for word, value in words_dict.items() if value == mx and len(word) == word_len]
    print(word_len, "letter words:", max_word_list, mx)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 2019-10-13
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多