小编Dei*_*eik的帖子

将多字典转换为具有重复键值的字典作为列表

我正在使用 Aiohttp 的 multidict() 实现。拿着它:

>>> d = MultiDict[('a', 1), ('b', 2), ('a', 3)])
>>> d
<MultiDict {'a': 1, 'b': 2, 'a': 3}>
Run Code Online (Sandbox Code Playgroud)

我想转换d为常规字典,其中重复的键值被附加到列表中,如下所示:

{'a': [1, 3], 'b': 2}
Run Code Online (Sandbox Code Playgroud)

有没有一种优雅的方法来转换它?除了循环遍历项目和大量逻辑条件之外?

python dictionary aiohttp

7
推荐指数
1
解决办法
2336
查看次数

标签 统计

aiohttp ×1

dictionary ×1

python ×1