简体   繁体   English

将字典列表合并为一个的更多 Pythonic 方式?

[英]More Pythonic Way to Merge a list of dictionaries into one?

Is there a more Pythonic way to do this?有没有更 Pythonic 的方式来做到这一点? I know there must be.我知道一定有。

for form in forms:
   d[form.keys()[0]] = form.values()[0]

Thanks!谢谢!

If you want d to have all the key-value pairs of the dictionaries in forms :如果您希望d拥有forms中字典的所有键值对:

for form in forms:
    d.update(form)
reduce(lambda acc,form: dict(acc,**form),forms)

PS: I generally use dict(x,**y) to merge to dictionaries than x.update(y) since most of the time I don't want the changes to be inplace. PS:我通常使用dict(x,**y)来合并字典而不是x.update(y)因为大多数时候我不希望更改就位。 This doesn't matter in this case though.不过在这种情况下这并不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM