我有一个输入字典,如下所示:
{"payment":
{"payment_id": "AAHPW34190", "clm_list":
{"dtl":
[{"clm_id": "1A2345"},
{"clm_id": "9999"}
]},
"payment_amt": "20"}}
我需要输出看起来像这样:
{ "create":
{ "_index": "website", "_type": "blog", "_id": "AAHPW34190"}}
{"payment_id": "AAHPW34190", "clm_list":
{"dtl":
[{"clm_id": "1A2345"},
{"clm_id": "9999"}
]},
"payment_amt": "20"}
第一行输出中的_id值来自payment_id。 通过执行以下操作,我可以轻松获得上述输出:
static_line={ "create": { "_index": "website", "_type": "blog", "_id": "0"}}
orig={"payment": {"payment_id": "AAHPW34190", "clm_list": {"dtl": [{"clm_id": "1A2345"}, {"clm_id": "9999"}]}, "payment_amt": "20"}}`
sec_line=orig["payment"]
static_line["_id"]=sec_line["payment_id"]`
但是我的输入将是一百万个dict元素,我希望尽可能高效地完成它。 那么,对于一百万个dicts,我能做得更好吗?
答案 0 :(得分:0)
我重新格式化输入字典的输出。假设下面的dict是列表的一个元素,其中有数百万个字典。
{"payment": {"payment_id": "AAHPW34190",
"clm_list": {"dtl": [{"clm_id": "1A2345"}, {"clm_id":"9999"}]},
"payment_amt": "20"}
}
使用列表推导来提取payment_id并创建新词典
[{ "create": { "_index": "website", "_type": "blog", "_id": d['payment']['payment_id']}}
for d in my_list_of_dict]