python - 遍历 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()
解决方案
罪魁祸首:
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']]
推荐阅读
- powershell - 在 Powershell 终端中执行基于环境变量的路径
- php - PHP,解析特定命名空间的所有类,并列出这些类的所有方法
- hadoop - 数据节点因 org.apache.hadoop.util.DiskChecker$DiskErrorException 而失败
- javascript - AJAX 内容加载后需要重新加载多个脚本
- http - Github API:远程主机在启动入站响应之前关闭了连接
- go - VSCODE gopath 与 shell 中设置的 GOPATH 不同
- dart - 如何从 Dart 中的 ByteData 读取数据(int32、int16 或单字节)
- python - 在 Windows 8 上创建的 pyinstaller exe 在 Windows 7 中不起作用
- angularjs - 单击按钮时正式更新模板选项
- mysql - MySQL 公式将线性刻度转换为对数刻度