Python遍历多值嵌套字典 [英] Python iterate over multi value nested dictionary

查看:111
本文介绍了Python遍历多值嵌套字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的数据结构,称为letters_dict:

I have the below data structure call letters_dict:

'aa': {'Price': '147,130,104,24,19',
       'Qty': '262,53,65,80,185,210',
       'Time': '51302324915,51308461317,51316258845,51324326568'},

'bb': {'Price': '196,203,209,177,150,160,160,180,194',
       'Qty': '129,268,225,228,176,76,17,45,207,61,143,195,230,97',
       'Time': '51305086913,51314981179,51323072726,51435766657,51597990966'}}

我想遍历外部词典的每个项目,然后遍历内部值,价格数量和时间,然后遍历价格数量和时间之内的每个值,然后进行数据处理.最好的方法是什么?

I want to iterate over each item of the outer dictionary and then the inner values, price qty and time and then each value within price qty and time to then do data processing. What is the best way to do this?

推荐答案

首先使用key_level1和val_level1对外部值进行迭代,然后使用key_level2,val_level2对内部值进行迭代:

First iterating over the outer values using key_level1 and val_level1, then iterating over inner values using key_level2, val_level2:

for key_level1, val_level1 in r.items():
        for key_level2, val_level2 in val_level1.items():
            for val in val_level2.split(','):
                # Example:
                # do something
                print(key_level1, key_level2, val)

这篇关于Python遍历多值嵌套字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆