python-3.x - 具有嵌套列表值的字典到单个值列表
问题描述
我正在尝试为每个键在字典中创建单个值列表。贝娄是问题。我正在尝试在 Jinja 中解析列表,并且在使用模板之前将它们转换为一个看起来更好。
问题:
{'EFTPOS': [[10.0, 5.0], 15.0], 'StoreDeposit': [[5.0, 6.0], 11.0]}
结果:
{'EFTPOS': [10.0, 5.0, 15.0], 'StoreDeposit': [5.0, 6.0, 11.0]}
解决方案
请尝试此代码片段。我已经定义了一种方法来删除嵌套列表并将其转换为平面列表。
output = []
def rmNest(ls):
for i in ls:
if type(i) == list:
rmNest(i)
else:
output.append(i)
return output
a_dict = {'EFTPOS': [[10.0, 5.0], 15.0], 'StoreDeposit': [[5.0, 6.0], 11.0]}
new_dict = {}
for i in a_dict:
new_dict[i] = rmNest(a_dict[i])
output = []
推荐阅读
- bootstrap-4 - 如何在引导程序 4 中使用响应式图像?
- multithreading - PlanningEntity 中的多线程与 ValueRangeProvider
- c++ - Qt找不到创建的目录
- c# - 匹配列表中的字符串并将匹配对象的其他属性与 LINQ 一起使用
- nestjs - Nestjs通过swagger模块招摇表单数据
- asp.net-mvc - 使用 ajax (asp.net,mvc) 加载 jQuery DataTable
- android - 如何减小 Android 数据库的大小
- archilogic - aframe:轨道控制不适用于 3dio.js
- python - 如何删除 csv 文件的重复基键
- c - 让 Eclipse 识别 Makefile 中定义的符号