[英]How to reduce convert list of Dict to Dict in Python
I have a requirement to convert list of dict in below format.我需要以以下格式转换 dict 列表。
[{"key":"eff_date","value":"20202020"},{"key":"member_id","value":"33sasfafXCC"},{"key":"exp_date","value":"20992020"}]
into this format:变成这种格式:
{"eff_date":"20202020","member_id":"33sasfafXCC","exp_date":"20992020"}
I tried reduce function with below but couldn't quite getting it.我尝试使用以下减少功能,但无法完全理解。
payload = [{"key":"eff_date","value":"20202020"},{"key":"member_id","value":"33sasfafXCC"},{"key":"exp_date","value":"20992020"}]
list = []
def keys(x,y):
return list.append({y['key'] : y['value']})
result = reduce(keys, payload,None)
You don't really need reduce
.你真的不需要
reduce
。 You aren't reduce
-ing anything.你没有
reduce
任何东西。
Try a simple dictionary comprehension:尝试一个简单的字典理解:
result = {p["key"]:p["value"] for p in payload}
>>> result
{'eff_date': '20202020', 'member_id': '33sasfafXCC', 'exp_date': '20992020'}
You can edit your function to look as so:你可以编辑你的函数看起来像这样:
payload = [{"key":"eff_date","value":"20202020"},{"key":"member_id","value":"33sasfafXCC"},{"key":"exp_date","value":"20992020"}]
def change(list):
newDict = {}
for dict in payload:
newDict[dict["key"]] = dict["value"]
return newDict
result = change(payload)
print(result)
Output:输出:
{'eff_date': '20202020', 'member_id': '33sasfafXCC', 'exp_date': '20992020'}
This will take in a list of dictionaries, iterate through every dictionary, add the key-value pair into a new dictionary, and then return that dictionary.这将接收一个字典列表,遍历每个字典,将键值对添加到新字典中,然后返回该字典。
As @not_speshal mentioned, you don't need the built-in reduce
function.正如@not_speshal 提到的,您不需要内置的
reduce
函数。
Note, you could do this with functools.reduce
, although, you really should just use a dictionary comprehension, but one efficient solution would be:请注意,您可以使用
functools.reduce
来执行此操作,尽管您确实应该只使用字典理解,但一种有效的解决方案是:
>>> from functools import reduce
>>> data = [{"key":"eff_date","value":"20202020"},{"key":"member_id","value":"33sasfafXCC"},{"key":"exp_date","value":"20992020"}]
>>> def reducer(acc, x):
... acc[x['key']] = x['value']
... return acc
...
>>> reduce(reducer, data, {})
{'eff_date': '20202020', 'member_id': '33sasfafXCC', 'exp_date': '20992020'}
Which is not pretty - it relies on side-effects.这并不漂亮 - 它依赖于副作用。 Might as well just be a for-loop if you are going to rely on side-effects.
如果您要依赖副作用,也可能只是一个 for 循环。
A more "principled" way that doesn't rely on side-effects using the new pip-update operator |
一种更“有原则”的方式,不依赖于使用新的 pip-update 运算符的副作用
|
would be:将会:
>>> def reducer(acc, x):
... return acc | {x['key']:x['value']}
...
>>> reduce(reducer, data, {})
{'eff_date': '20202020', 'member_id': '33sasfafXCC', 'exp_date': '20992020'}
But this would be unnecessarily inefficient, and would scale poorly.但这会不必要地低效,并且扩展性很差。
So in summary, the most natural way to do this in Python is simply:所以总而言之,在 Python 中最自然的方法就是:
result = {x['key']: x['value'] for x in data}
Or even just the for-loop form:甚至只是 for 循环形式:
result = {}
for x in data:
result[x['key']] = x['value']
Would be much preferable to either reduce
based solution最好是基于
reduce
的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.