首页 > 解决方案 > 遍历 Python 字典并特殊追加到新列表?

问题描述

我想遍历一个字典,并将每个键(字母)按照其值(频率)的次数重复附加到一个新列表中。

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

我在做什么是行不通的。我在我的函数中写什么来做到这一点?

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()

标签: pythonloopsdictionaryappendkey-value

解决方案


罪魁祸首

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']

或者

如果你想让它们很好地配对

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

OP但是我仍然遇到打印输出的问题。它给了我我正在寻找的东西,但也给了我最上面的原始字典

Ans:因为您同时打印 thedict和 the list

print(freq_dict, freq_lst)

只需打印list

print(freq_lst)

编辑2:

另一种将相似元素组合在一起的更好方法,使用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']]

推荐阅读