将列表转换为字典,然后通过键值将多个字典合并为一个字典

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将列表转换为字典,然后通过键值将多个字典合并为一个字典相关的知识,希望对你有一定的参考价值。

我有3个要插入字典的python列表,然后根据键值将这3个字典合并为一个。

我的python列表是这样制作的:

with open ('full_product_shipments.xml','r') as file2:
    full_product_shipments = list([line.strip().replace('"','').replace('','').replace('"','').replace(':',',').split(',') for line in file2])

它们看起来像这样:

列出一个

[['transaction_id', '224847627', 'product_amount', '2.73', 'user_invoice_date', '2018-12-21'],
['transaction_id', '67919397', 'product_amount', '2.73', 'user_invoice_date', '2017-10-26']

清单二

[['tracking_code', '29285908', 'from_country', 'FR', 'to_country', 'FR', 'package_type_id', '10', 'transaction_id', '172238850', 'shipping_label_created', '2018-09-25 18', '40', '52'],
['tracking_code', '22105784', 'from_country', 'FR', 'to_country', 'FR', 'package_type_id', '10', 'transaction_id', '111423825', 'shipping_label_created', '2018-04-13 11', '22', '44']]

清单三

[['tracking_code', '21703238', 'from_country', 'FR', 'to_country', 'FR', 'amount', '3.23'],
['tracking_code', '41545695', 'from_country', 'FR', 'to_country', 'FR', 'amount', '2.9']]

清单一和二都具有transaction_id,一旦将它们转换成字典,我就需要在它们上加入它们。

[新加入的列表(一和二)和列表三都具有tracking_id,一旦列表三被转换成字典,我想通过它们加入它们。

我尝试使用此:

result=[x.update(amount=y['amount']) for x in full_product_shipments for y in full_provider_invoices if x['transaction_id'] == y['transaction_id']]

但是这会抛出TypeError:

TypeError: list indices must be integers or slices, not str

也许没有必要将所有内容都转换为dict,我是python的新手,所以如果有更好的方法来基于键合并信息,我将不胜感激。

答案
l1 = [['transaction_id', '224847627', 'product_amount', '2.73', 'user_invoice_date', '2018-12-21'], ['transaction_id', '67919397', 'product_amount', '2.73', 'user_invoice_date', '2017-10-26']] l2 = [['tracking_code', '29285908', 'from_country', 'FR', 'to_country', 'FR', 'package_type_id', '10', 'transaction_id', '172238850', 'shipping_label_created', '2018-09-25 18', '40', '52'], ['tracking_code', '22105784', 'from_country', 'FR', 'to_country', 'FR', 'package_type_id', '10', 'transaction_id', '111423825', 'shipping_label_created', '2018-04-13 11', '22', '44']] l3 = [['tracking_code', '21703238', 'from_country', 'FR', 'to_country', 'FR', 'amount', '3.23'], ['tracking_code', '41545695', 'from_country', 'FR', 'to_country', 'FR', 'amount', '2.9']] # Convert everything to dict result = y['transaction_id']:y for y in [dict(zip(x[::2], x[1::2])) for x in l1] d2 = y['transaction_id']:y for y in [dict(zip(x[::2], x[1::2])) for x in l2] d3 = y['tracking_code']:y for y in [dict(zip(x[::2], x[1::2])) for x in l3] # Update result dict with data from the other lists. for entry in result.values(): entry.update(d2[entry['transaction_id']]) entry.update(d3[entry['tracking_code']])
另一答案
[为了分享我在工作中的经验,我得到了一个SAP API,默认响应为XML…我试图使用Python XML解析库(无休止地打扰我)来理解它,直到我意识到我可以通过一个原始URL字符串的参数,它会以JSON的形式返回。根据我的经验,这是我对您的问题的建议。

这里是带有语法的公共API的示例……尝试为您的API尝试类似的组合。

https://vpic.nhtsa.dot.gov/api/

https://vpic.nhtsa.dot.gov/api/Home/Index/LanguageExamples

现在,如果您可以下载JSON字符串,将其转换为Python字典就很容易了……在线上有很多资源来做到这一点。然后将Python字典转换为pandas数据框非常简单,在线上有很多资源如何做到这一点。然后将多个数据框连接在一起非常简单,在线资源很多,如何做到这一点。

如果无法获取JSON字符串,则在线上有一些(更复杂的)资源,介绍了如何从XML转换为JSON。这里有一些链接:

How to convert an xml string to a dictionary?

https://ericscrivner.me/2015/07/python-tip-convert-xml-tree-to-a-dictionary/

http://code.activestate.com/recipes/573463-converting-xml-to-dictionary-and-back/

您会发现使用字典而不是列表要容易得多。列表用于存储订购的商品,但是列表中存储了一堆键/值对(这正是字典的用处)。

希望有帮助!

以上是关于将列表转换为字典,然后通过键值将多个字典合并为一个字典的主要内容,如果未能解决你的问题,请参考以下文章

每个键有多个值的列表到字典转换?

将标准 python 键值字典列表转换为 pyspark 数据框

如果它们共享任何键值对,如何合并来自不同列表的多个字典?

Python列表元素为字典时,如何根据其中某个相同的键值进行元素合并

如何使用列表和字典将字符串转换为二进制代码

如何将空格分隔的键值对字符串转换为字典