【发布时间】:2021-06-28 06:03:21
【问题描述】:
我有一个 JSON 对象列表,已经排序(比如说按时间排序)。每个 JSON 对象都有type 和status。例如:
[
{'type': 'A', 'status': 'ok'},
{'type': 'B', 'status': 'ok'},
{'type': 'A', 'status': 'fail'}
]
我想把它转换成:
{
'A': ['ok', 'fail'],
'B': ['ok']
}
所以这当然是一项简单的任务,但我正在寻找 Pythonic 的方式来做到这一点,所以它可以节省我几行代码
【问题讨论】:
-
也许可以分享您当前编写的代码,然后人们可以为您提供有关如何使其更好或更pythonic的指示
标签: python python-3.x list-comprehension dictionary-comprehension