如何以有效的方式简化我的代码?

时间:2016-10-06 08:45:29

标签: python list dictionary

所以目前我仍然坚持我的任务问题, 作业问题是: 定义print_most_frequent()函数,该函数传递两个参数,一个包含单词及其相应频率的字典(它们在一串文本中出现的次数),例如,

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

和整数,字典中要考虑的关键字的长度。

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

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

print_most_frequent(word_frequencies,3)   
print_most_frequent(word_frequencies,4)
print_most_frequent(word_frequencies,5)
print_most_frequent(word_frequencies,6)
print_most_frequent(word_frequencies, 7) 

打印以下内容:

3 letter keywords: ['bat', 'cat'] 9
4 letter keywords: ['fish', 'frog'] 9
5 letter keywords: ['stork'] 1
6 letter keywords: ['parrot'] 8
7 letter keywords: [] 0

我已编码得到上面的答案,但它说我错了。也许它需要一个简化但我正在努力的方法。有人帮忙谢谢你。

def print_most_frequent(words_dict, word_len):
    word_list = []
    freq_list = []
    for word,freq in words_dict.items():
        if len(word) == word_len:
            word_list += [word]
            freq_list += [freq]
    new_list1 = []
    new_list2 = []
    if word_list == [] and freq_list == []:
        new_list1 += []
        new_list2 += [0]
        return print(new_list1, max(new_list2))
    else:
        maximum_value = max(freq_list)
        for i in range(len(freq_list)):
            if freq_list[i] == maximum_value:
                new_list1 += [word_list[i]]
                new_list2 += [freq_list[i]]
                new_list1.sort()
        return print(new_list1, max(new_list2))

2 个答案:

答案 0 :(得分:0)

您可以使用:

def print_most_frequent(words_dict, word_len):
    max_freq = 0
    words = list()
    for word, frequency in words_dict.items():
        if len(word) == word_len:
            if frequency > max_freq:
                max_freq = frequency
                words = [word]
            elif frequency == max_freq:
                words.append(word)
    print("{} letter keywords:".format(word_len), sorted(words), max_freq)

它只是迭代单词字典,仅考虑长度为所需单词的单词并构建最常用单词的列表,一旦找到更高的频率就重置它。

答案 1 :(得分:0)

您可以做的一种方法是将值映射为键,反之亦然,这样您就可以轻松获得最常用的词:

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

getfunc = lambda x, dct: [i for i in dct if dct[i] == x]
new_dict = { k : getfunc(k, a) for k in a.values() }

print (new_dict)

输出:

{8: ['parrot'], 1: ['stork'], 4: ['rat', 'dog'], 9: ['bat', 'fish', 'frog', 'cat']}

所以,现在如果你想要9个数字,只需说

b = new_dict[9]
print (b, len(b))

将给出:

['cat', 'fish', 'bat', 'frog'] 4

你可以使用字典,而不是再次调用函数。你只需要在频率上循环一次就会更快,但是如果你仍然需要一个函数,那么可以做一个单线lambda:

print_most_frequent = lambda freq, x: print (freq[x])

print_most_frequent(new_dict, 9)
print_most_frequent(new_dict, 4)

给出:

['fish', 'bat', 'frog', 'cat']
['rat', 'dog']