简体   繁体   English

如何在 Python 中遍历此列表

[英]How can I iterate through this list in Python

I'm trying through iterate through the list below and am having a hard time.我正在尝试遍历下面的列表,但遇到了困难。 I am trying to access the dictionary element "metrics".我正在尝试访问字典元素“metrics”。 I am trying to print out all the key/values of 'timestamp' and 'values'.我正在尝试打印出“时间戳”和“值”的所有键/值。

I've been able to access the list using the code below and do see how I can individually get within the list but am not sure how to build a loop to get all values I need.我已经能够使用下面的代码访问列表,并查看我如何单独进入列表,但不确定如何构建一个循环来获取我需要的所有值。

flat_file = DATA[0]['environments'][0]['dimensions'][0]

[
    {
        "environments": [
            {
                "dimensions": [
                    {
                        "metrics": [
                            {
                                "name": "sum(message_count)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "651.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "510.0"
                                    }
                                ]
                            },
                            {
                                "name": "global-avg-request_size",
                                "values": [
                                    "0.0"
                                ]
                            },
                            {
                                "name": " avg(request_size)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "0.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "0.0"
                                    }
                                ]
                            }
                        ],
                        "name": "TestL"
                    },
                    {
                        "metrics": [
                            {
                                "name": "sum(message_count)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "477.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "338.0"
                                    }
                                ]
                            },
                            {
                                "name": "global-avg-request_size",
                                "values": [
                                    "0.0"
                                ]
                            },
                            {
                                "name": " avg(request_size)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "0.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "0.0"
                                    }
                                ]
                            }
                        ],
                        "name": "test_y"
                    },
                    {
                        "metrics": [
                            {
                                "name": "sum(message_count)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "91.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "93.0"
                                    }
                                ]
                            },
                            {
                                "name": "global-avg-request_size",
                                "values": [
                                    "0.0"
                                ]
                            },
                            {
                                "name": " avg(request_size)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "0.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "0.0"
                                    }
                                ]
                            }
                        ],
                        "name": "Testz"
                    }
                ],
                "name": "prod"
            }
        ],
        "metaData": {
            "errors": [],
            "notices": [
                "query served by: something",
                "Source:db",
                "Table used: something",
                "Metric with Avg of request_size was requested. For this a global avg was also computed with name global-avg-request_size"
            ]
        }
    }
]

This will work:这将起作用:

for i in range(len(l[0]['environments'][0]['dimensions'])):
    for j in range(len(l[0]['environments'][0]['dimensions'][i]['metrics'])):
        if '{' in str(l[0]['environments'][0]['dimensions'][i]['metrics'][j]['values']):
            print(l[0]['environments'][0]['dimensions'][i]['metrics'][j]['values'])

Output:输出:

[{'timestamp': 1582588800000, 'value': '651.0'}, {'timestamp': 1582502400000, 'value': '510.0'}]
[{'timestamp': 1582588800000, 'value': '0.0'}, {'timestamp': 1582502400000, 'value': '0.0'}]
[{'timestamp': 1582588800000, 'value': '477.0'}, {'timestamp': 1582502400000, 'value': '338.0'}]
[{'timestamp': 1582588800000, 'value': '0.0'}, {'timestamp': 1582502400000, 'value': '0.0'}]
[{'timestamp': 1582588800000, 'value': '91.0'}, {'timestamp': 1582502400000, 'value': '93.0'}]
[{'timestamp': 1582588800000, 'value': '0.0'}, {'timestamp': 1582502400000, 'value': '0.0'}]

What you are reading is JSON, to read it in python you need :您正在阅读的是 JSON,要在 Python 中阅读它,您需要:

import json

data = '''{
        "environments": [
            {
                "dimensions": [
                    {
                        "metrics": [
                            {
                                "name": "sum(message_count)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "651.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "510.0"
                                    }
                                ]
                            },
                            {
                                "name": "global-avg-request_size",
                                "values": [
                                    "0.0"
                                ]
                            },
                            {
                                "name": " avg(request_size)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "0.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "0.0"
                                    }
                                ]
                            }
                        ],
                        "name": "TestL"
                    },
                    {
                        "metrics": [
                            {
                                "name": "sum(message_count)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "477.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "338.0"
                                    }
                                ]
                            },
                            {
                                "name": "global-avg-request_size",
                                "values": [
                                    "0.0"
                                ]
                            },
                            {
                                "name": " avg(request_size)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "0.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "0.0"
                                    }
                                ]
                            }
                        ],
                        "name": "test_y"
                    },
                    {
                        "metrics": [
                            {
                                "name": "sum(message_count)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "91.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "93.0"
                                    }
                                ]
                            },
                            {
                                "name": "global-avg-request_size",
                                "values": [
                                    "0.0"
                                ]
                            },
                            {
                                "name": " avg(request_size)",
                                "values": [
                                    {
                                        "timestamp": 1582588800000,
                                        "value": "0.0"
                                    },
                                    {
                                        "timestamp": 1582502400000,
                                        "value": "0.0"
                                    }
                                ]
                            }
                        ],
                        "name": "Testz"
                    }
                ],
                "name": "prod"
            }
        ],
        "metaData": {
            "errors": [],
            "notices": [
                "query served by: something",
                "Source:db",
                "Table used: something",
                "Metric with Avg of request_size was requested. For this a global avg was also computed with name global-avg-request_size"
            ]
        }
    }'''

data_dict = json.loads(data)

This will create a python dictionary of your data.这将为您的数据创建一个 python 字典。 So now access the data as you will for a dictionary.所以现在像访问字典一样访问数据。

like: data_dict["environments"]像: data_dict["environments"]

to go deeper try combo of for:更深入地尝试组合 for:

>>> for doc in data_dict['environments']:
...   for i in doc['dimensions']:
...     print(i['name'])
...
TestL
test_y
Testz

For better data oriented view and usage use Pandas check this link and this为了更好的面向数据的视图和使用使用熊猫检查这个链接和这个

lisp = [
{
    "environments": [
        {
            "dimensions": [
                {
                    "metrics": [
                        {
                            "name": "sum(message_count)",
                            "values": [
                                {
                                    "timestamp": 1582588800000,
                                    "value": "651.0"
                                },
                                {
                                    "timestamp": 1582502400000,
                                    "value": "510.0"
                                }
                            ]
                        },
                        {
                            "name": "global-avg-request_size",
                            "values": [
                                "0.0"
                            ]
                        },
                        {
                            "name": " avg(request_size)",
                            "values": [
                                {
                                    "timestamp": 1582588800000,
                                    "value": "0.0"
                                },
                                {
                                    "timestamp": 1582502400000,
                                    "value": "0.0"
                                }
                            ]
                        }
                    ],
                    "name": "TestL"
                },
                {
                    "metrics": [
                        {
                            "name": "sum(message_count)",
                            "values": [
                                {
                                    "timestamp": 1582588800000,
                                    "value": "477.0"
                                },
                                {
                                    "timestamp": 1582502400000,
                                    "value": "338.0"
                                }
                            ]
                        },
                        {
                            "name": "global-avg-request_size",
                            "values": [
                                "0.0"
                            ]
                        },
                        {
                            "name": " avg(request_size)",
                            "values": [
                                {
                                    "timestamp": 1582588800000,
                                    "value": "0.0"
                                },
                                {
                                    "timestamp": 1582502400000,
                                    "value": "0.0"
                                }
                            ]
                        }
                    ],
                    "name": "test_y"
                },
                {
                    "metrics": [
                        {
                            "name": "sum(message_count)",
                            "values": [
                                {
                                    "timestamp": 1582588800000,
                                    "value": "91.0"
                                },
                                {
                                    "timestamp": 1582502400000,
                                    "value": "93.0"
                                }
                            ]
                        },
                        {
                            "name": "global-avg-request_size",
                            "values": [
                                "0.0"
                            ]
                        },
                        {
                            "name": " avg(request_size)",
                            "values": [
                                {
                                    "timestamp": 1582588800000,
                                    "value": "0.0"
                                },
                                {
                                    "timestamp": 1582502400000,
                                    "value": "0.0"
                                }
                            ]
                        }
                    ],
                    "name": "Testz"
                }
            ],
            "name": "prod"
        }
    ],
    "metaData": {
        "errors": [],
        "notices": [
            "query served by: something",
            "Source:db",
            "Table used: something",
            "Metric with Avg of request_size was requested. For this a global avg was also computed with name global-avg-request_size"
        ]
    }
}
]

for element in lisp[0]['environments'][0]['dimensions']:

    for inner_element in element['metrics']:

        for el in inner_element['values']:

            if type(el) == dict:
                print(el)

{'timestamp': 1582588800000, 'value': '651.0'}
{'timestamp': 1582502400000, 'value': '510.0'}
{'timestamp': 1582588800000, 'value': '0.0'}
{'timestamp': 1582502400000, 'value': '0.0'}
{'timestamp': 1582588800000, 'value': '477.0'}
{'timestamp': 1582502400000, 'value': '338.0'}
{'timestamp': 1582588800000, 'value': '0.0'}
{'timestamp': 1582502400000, 'value': '0.0'}
{'timestamp': 1582588800000, 'value': '91.0'}
{'timestamp': 1582502400000, 'value': '93.0'}
{'timestamp': 1582588800000, 'value': '0.0'}
{'timestamp': 1582502400000, 'value': '0.0'}

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

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