如何遍历字典列表?



大家好,我有一个字典,一个键值对,它的值有一个字典列表:

tables = {
"aaa.acs": [
{"2021-02-04 02:06:00-05:00": 0},
{"2021-02-04 02:07:00-05:00": 0},
{"2021-02-04 02:08:00-05:00": 0},
{"2021-02-04 02:09:00-05:00": 0},
{"2021-02-04 02:10:00-05:00": 0},
{"2021-02-04 02:11:00-05:00": 0},
{"2021-02-04 02:12:00-05:00": 0},
{"2021-02-04 02:13:00-05:00": 0},
{"2021-02-04 02:14:00-05:00": 0},
{"2021-02-04 02:15:00-05:00": 0},
{"2021-02-04 02:16:00-05:00": 0},
{"2021-02-04 02:17:00-05:00": 0},
{"2021-02-04 02:18:00-05:00": 0},
{"2021-02-04 02:19:00-05:00": 0},
{"2021-02-04 02:20:00-05:00": 0},
{"2021-02-04 02:21:00-05:00": 0},
{"2021-02-04 02:22:00-05:00": 0},
{"2021-02-04 02:23:00-05:00": 0},
{"2021-02-04 02:24:00-05:00": 0},
{"2021-02-04 02:25:00-05:00": 0},
{"2021-02-04 02:26:00-05:00": 0},
{"2021-02-04 02:27:00-05:00": 0},
{"2021-02-04 02:28:00-05:00": 0},
{"2021-02-04 02:29:00-05:00": 0},
{"2021-02-04 02:30:00-05:00": 0},
{"2021-02-04 02:31:00-05:00": 0},
{"2021-02-04 02:32:00-05:00": 0},
{"2021-02-04 02:33:00-05:00": 0},
{"2021-02-04 02:34:00-05:00": 0},
{"2021-02-04 02:35:00-05:00": 0},
],
"accss.msft": [
{"2021-02-04 02:06:00-05:00": 0},
{"2021-02-04 02:07:00-05:00": 0},
{"2021-02-04 02:08:00-05:00": 0},
{"2021-02-04 02:09:00-05:00": 0},
{"2021-02-04 02:10:00-05:00": 0},
{"2021-02-04 02:11:00-05:00": 0},
{"2021-02-04 02:12:00-05:00": 0},
{"2021-02-04 02:13:00-05:00": 0},
{"2021-02-04 02:14:00-05:00": 0},
{"2021-02-04 02:15:00-05:00": 0},
{"2021-02-04 02:16:00-05:00": 0},
{"2021-02-04 02:17:00-05:00": 0},
{"2021-02-04 02:18:00-05:00": 0},
{"2021-02-04 02:19:00-05:00": 0},
{"2021-02-04 02:20:00-05:00": 0},
{"2021-02-04 02:21:00-05:00": 0},
{"2021-02-04 02:22:00-05:00": 0},
{"2021-02-04 02:23:00-05:00": 0},
{"2021-02-04 02:24:00-05:00": 0},
{"2021-02-04 02:25:00-05:00": 0},
{"2021-02-04 02:26:00-05:00": 0},
{"2021-02-04 02:27:00-05:00": 0},
{"2021-02-04 02:28:00-05:00": 0},
{"2021-02-04 02:29:00-05:00": 0},
{"2021-02-04 02:30:00-05:00": 0},
{"2021-02-04 02:31:00-05:00": 0},
{"2021-02-04 02:32:00-05:00": 0},
{"2021-02-04 02:33:00-05:00": 0},
{"2021-02-04 02:34:00-05:00": 0},
{"2021-02-04 02:35:00-05:00": 0},
],
}

我已经尝试了这个方法来获得它,但是我对这个性能不满意:

for test in tables.values():
for value in test:
for time, cost in value.items():
print(time, cost)

如何访问这个字典列表的键/值对?我正在考虑是否有可能将其从列表转换为字典以获得更好的性能。你们认为我有什么方法可以实现这个逻辑?

from itertools import chain
for your_dict in chain(*tables.values()):
key, value = tuple(*your_dict.items())
print(key, value)

*tables .values()将把所有列表解包到一个可迭代对象中,迭代时将给出字典。
tuple(*your_dict.items())将键值对解包成元组

相关内容

  • 没有找到相关文章

最新更新