遍历 Python 字典并特殊追加到新列表? [英] Iterate through Python dictionary and special append to new list?
本文介绍了遍历 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']
或
如果您希望它们完美配对:
for i in range(value):
freq_lst.append([key]*value)
OP:但是我在打印输出时仍然遇到问题.它给了我我正在寻找的东西,还有最上面的原始字典
Ans:因为您要打印 dict
和 list
:
Ans: Because you're printing both the dict
and the list
:
print(freq_dict, freq_lst)
只需打印list
:
print(freq_lst)
编辑 2:
另一种将相似元素组合在一起的更好方法,使用 groupby()
:
Another nicer way of grouping the similar elements together, using groupby()
:
仅附加键
:
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屋!
查看全文