遍历Python词典并将特殊追加到新列表? [英] Iterate through Python dictionary and special append to new list?

查看:187
本文介绍了遍历Python词典并将特殊追加到新列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历字典,然后将每个键(字母)重复其值(频率)的次数添加到新列表中.

I would like to iterate over a dictionary, and append each key (letter) repeated by the number of times of its value (frequency) to a new list.

例如: 输入:{'A':1, 'B':2}.预期输出:['A', 'B', 'B']

For example: input: {'A':1, 'B':2}. Expected output: ['A', 'B', 'B']

我正在做的事没有用.我要在函数中写些什么来做到这一点?

What I'm doing is not working. What do I write in my function to do this?

def get_freq_dict():
    freq_dict = {'J' : 1, 'K' : 1, 'Q' : 1, 'X' : 1, 'Z' : 1,\
                'B' : 2, 'C' : 2, 'F' : 2, 'H' : 2, 'M' : 2, 'P' : 2,\
                'V' : 2, 'W' : 2, 'Y' : 2, '' : 2,\
                'G' : 3, 'D' : 4, 'L' : 4, 'S' : 4, 'U' : 4,\
                'N' : 6, 'R' : 6, 'T' : 6, 'O' : 8, 'A' : 9, 'I' : 9,\
                'E' : 12}
    return freq_dict


def bag_of_letters(freq_dict):
    freq_lst = [] 
    for key, value in freq_dict.items():
        for range in(value):
            freq_lst.append(value)
    return freq_lst


def main():

    freq_dict = get_freq_dict()
    freq_lst = bag_of_letters(freq_dict)

    print(freq_dict, freq_lst)
main()

推荐答案

元凶:

for range in(value):
    freq_lst.append(value)

救助者:

for i in range(value):
     freq_lst.append(key)

因此:

def get_freq_dict():
    freq_dict = {'J' : 1, 'K' : 1, 'Q' : 1, 'X' : 1, 'Z' : 1,\
                'B' : 2, 'C' : 2, 'F' : 2, 'H' : 2, 'M' : 2, 'P' : 2,\
                'V' : 2, 'W' : 2, 'Y' : 2, '' : 2,\
                'G' : 3, 'D' : 4, 'L' : 4, 'S' : 4, 'U' : 4,\
                'N' : 6, 'R' : 6, 'T' : 6, 'O' : 8, 'A' : 9, 'I' : 9,\
                'E' : 12}
    return freq_dict


def bag_of_letters(freq_dict):
    freq_lst = []
    for key, value in freq_dict.items():
       # print(key, value)
        for i in range(value):
            freq_lst.append(key)
    return freq_lst


def main():

    freq_dict = get_freq_dict()
    freq_lst = bag_of_letters(freq_dict)

    print(freq_lst)
main()

输出:

['J', 'K', 'Q', 'X', 'Z', 'B', 'B', 'C', 'C', 'F', 'F', 'H', 'H', 'M', 'M', 'P', 'P', 'V', 'V', 'W', 'W', 'Y', 'Y', '', '', 'G', 'G', 'G', 'D', 'D', 'D', 'D', 'L', 'L', 'L', 'L', 'S', 'S', 'S', 'S', 'U', 'U', 'U', 'U', 'N', 'N', 'N', 'N', 'N', 'N', 'R', 'R', 'R', 'R', 'R', 'R', 'T', 'T', 'T', 'T', 'T', 'T', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E']

OR

如果您希望它们很好地配对:

 for i in range(value):
     freq_lst.append([key]*value)

OP :但是我仍然无法打印输出.它不仅提供了我要查找的内容,而且还提供了原始词典在顶部

答案:因为您要同时打印dictlist:

Ans: Because you're printing both the dict and the list:

print(freq_dict, freq_lst)

只需打印list即可:

print(freq_lst)

使用groupby()将相似元素分组在一起的另一种更好的方法:

Another nicer way of grouping the similar elements together, using groupby():

仅附加key:

 for i in range(0, value):
      freq_lst.append(key)

然后:

 print([list(j) for i, j in groupby(freq_lst)])

输出:

[['J'], ['K'], ['Q'], ['X'], ['Z'], ['B', 'B'], ['C', 'C'], ['F', 'F'], ['H', 'H'], ['M', 'M'], 
 ['P', 'P'], ['V', 'V'], ['W', 'W'], ['Y', 'Y'], ['', ''], ['G', 'G', 'G'], 
 ['D', 'D', 'D', 'D'], ['L', 'L', 'L', 'L'], ['S', 'S', 'S', 'S'], ['U', 'U', 'U', 'U'], 
 ['N', 'N', 'N', 'N', 'N', 'N'], ['R', 'R', 'R', 'R', 'R', 'R'], 
 ['T', 'T', 'T', 'T', 'T', 'T'], ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'], 
 ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A'], 
 ['I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I'], 
 ['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E']]

这篇关于遍历Python词典并将特殊追加到新列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆