使用字典理解转换Python字典

cru*_*vid 2 python dictionary dictionary-comprehension

我有以下Python字典:

{
 "cat": 1,
 "dog": 1,
 "person": 2,
 "bear": 2,
 "bird": 3
}
Run Code Online (Sandbox Code Playgroud)

我想使用字典理解将其转换为以下字典:

{
 1 : ["cat", "dog"],
 2 : ["person", "bear"],
 3 : ["bird"]
}
Run Code Online (Sandbox Code Playgroud)

我怎么能在一个班轮里做这件事?

bph*_*phi 5

这不是有效的,因为这不是如何使用dicts,但您可以执行以下操作

d = {"cat": 1, "dog": 1, "person": 2, "bear": 2, "bird": 3}

new = {v: [i[0] for i in d.items() if i[1] == v] for v in d.values()}
Run Code Online (Sandbox Code Playgroud)