gpt4 book ai didi

python - 遍历 Python 字典并将特殊 append 到新列表?

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:09 38 4
gpt4 key购买 nike

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

例如:输入:{'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()

最佳答案

罪魁祸首:

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:因为您同时打印了 dictlist:

print(freq_dict, freq_lst)

只需打印 list 即可:

print(freq_lst)

编辑 2:

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

仅 append :

 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 - 遍历 Python 字典并将特殊 append 到新列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54802168/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com