您有一个对象列表,每个对象都有一个id
属性。
下面是我将其转换为dict的方法,其中key是ids,value是对象:
reduce(
lambda x,y: dict(x.items() + { y.id : y}.items()),
list,
{}
)
建议更好的方法。
发布于 2010-06-18 22:07:30
dict([(x.id, x) for x in list])
发布于 2010-06-18 22:08:07
dict(map(lambda x: [x.id, x], list))
https://stackoverflow.com/questions/3070242
复制相似问题