简体   繁体   English

具有嵌套列表值的字典到单个值列表

[英]dictionary with nested list values to single list of values

I am trying to make single list of values in dictionary for each key.我正在尝试为每个键在字典中创建单个值列表。 Bellow is problem.贝娄是问题。 I am trying to parse list in Jinja and looks better to convert them into one before taking to template.我正在尝试在 Jinja 中解析列表,并且在使用模板之前将它们转换为一个看起来更好。

Problem:问题:

{'EFTPOS': [[10.0, 5.0], 15.0], 'StoreDeposit': [[5.0, 6.0], 11.0]}

Result:结果:

{'EFTPOS': [10.0, 5.0, 15.0], 'StoreDeposit': [5.0, 6.0, 11.0]}

Please try this code snippet.I have defined a method to remove the nested list and convert it into a flat list.请试试这个代码片段。我已经定义了一个删除嵌套列表并将其转换为平面列表的方法。

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 = []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM