Python 将列表转换为字典目录,然后按键值将多个字典目录合并为一个
我有三个python列表,我想把它们编成一个字典,然后根据键值将这三个字典合并成一个 我的python列表如下所示:Python 将列表转换为字典目录,然后按键值将多个字典目录合并为一个,python,list,dictionary,merge,Python,List,Dictionary,Merge,我有三个python列表,我想把它们编成一个字典,然后根据键值将这三个字典合并成一个 我的python列表如下所示: with open ('full_product_shipments.xml','r') as file2: full_product_shipments = list([line.strip().replace('{"','').replace('}','').replace('"','').replace(':',',').split(',') for line in
with open ('full_product_shipments.xml','r') as file2:
full_product_shipments = list([line.strip().replace('{"','').replace('}','').replace('"','').replace(':',',').split(',') for line in file2])
它们看起来是这样的:
列表1
[['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']]
列表2
[['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']]
列表3
[['tracking_code', '21703238', 'from_country', 'FR', 'to_country', 'FR', 'amount', '3.23'],
['tracking_code', '41545695', 'from_country', 'FR', 'to_country', 'FR', 'amount', '2.9']]
list1和list2都有transaction\u id
,一旦我将它们转换为dict,我就需要在其上加入它们
新加入的列表(list1和list2)和list3都有tracking\u id
,一旦list3转换为dict,我就想通过它加入它们
我试过用这个:
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: list indices must be integers or slices, not str
也许不需要将所有内容都转换为dict。我对python有点陌生,因此如果有更好的方法基于键合并信息,我将非常感谢学习它。示例数据中的示例似乎没有匹配的条目,假设您的完整数据集与您可以执行的任何操作都匹配
l1=['交易id'、'224847627'、'产品金额'、'2.73'、'用户发票日期'、'2018-12-21']、['交易id'、'67919397'、'产品金额'、'2.73'、'用户发票日期'、'2017-10-26']
l2=['跟踪代码','29285908','from_country','FR','to_country','FR','package_type_id','10','transaction_id','17238850','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=['跟踪代码','21703238','from_country','FR','to_country','FR','amount','3.23'],['跟踪代码','41545695','from_country','FR','to_country','FR','amount','2.9']]
#将所有内容转换为dict
结果={y['transaction_id']:y在[dict(zip(x[:2],x[1::2])中代表y在l1]中代表x}
d2={y['transaction_id']:y代表[dict(zip(x[::2],x[1::2]))中的y代表l2中的x]}
d3={y['tracking_code']:y代表y在[dict(zip(x[:2],x[1::2]))中,x代表x在l3]}
#使用其他列表中的数据更新结果dict。
对于result.values()中的条目:
条目.更新(d2[条目['transaction_id']]
条目.更新(d3[条目['tracking_code']]
假设您的完整数据集中有匹配项,那么示例数据中的示例似乎没有匹配项
l1=['交易id'、'224847627'、'产品金额'、'2.73'、'用户发票日期'、'2018-12-21']、['交易id'、'67919397'、'产品金额'、'2.73'、'用户发票日期'、'2017-10-26']
l2=['跟踪代码','29285908','from_country','FR','to_country','FR','package_type_id','10','transaction_id','17238850','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=['跟踪代码','21703238','from_country','FR','to_country','FR','amount','3.23'],['跟踪代码','41545695','from_country','FR','to_country','FR','amount','2.9']]
#将所有内容转换为dict
结果={y['transaction_id']:y在[dict(zip(x[:2],x[1::2])中代表y在l1]中代表x}
d2={y['transaction_id']:y代表[dict(zip(x[::2],x[1::2]))中的y代表l2中的x]}
d3={y['tracking_code']:y代表y在[dict(zip(x[:2],x[1::2]))中,x代表x在l3]}
#使用其他列表中的数据更新结果dict。
对于result.values()中的条目:
条目.更新(d2[条目['transaction_id']]
条目.更新(d3[条目['tracking_code']]
如果原始原始数据是“json”格式而不是“xml”格式,这将更容易加入。如果您使用REST API下载数据,请尝试在结尾输入关键字“&$format=json”,然后查看文件结果是否返回为json字符串。例如,这将在SAP REST API中工作,但我认为这是一个标准参数在众多API提供商中
为了分享我在工作中的经历,我得到了一个默认响应为XML的SAP API…我尝试使用Python XML解析库来理解它(无休止地绞尽脑汁)直到我意识到我可以将一个参数传递给原始URL字符串,它将作为JSON返回
下面是一个带有语法的公共API的示例……请尝试为您的API使用类似的组合
现在,如果你可以下载一个JSON字符串,那么将其转换为Python字典就很容易了…很多在线资源如何实现这一点。然后从Python字典转换为pandas dataframe非常简单,很多在线资源如何实现这一点。然后将多个dataframe连接在一起非常简单,只有很多资源不知道怎么做
如果您无法获取JSON字符串,那么在线上有一些(更复杂的)关于如何从XML转换为JSON的资源。以下是一些链接:
你会发现使用字典比使用列表容易得多。列表是用来存储有序项的,而列表是用来存储一组键值对(这正是字典的作用)
希望这会有所帮助!如果原始数据是“json”格式而不是“xml”,那么这将更容易加入。如果您使用REST API下载数据,请尝试在结尾输入一个关键字“&$format=json”,看看文件结果是否以json字符串的形式返回。例如,这将在SAP REST API中工作,但我认为这是一个sta许多API提供程序中的标准参数 为了分享我的工作经验,我得到了一个默认响应为XML的SAP API…我试图理解
from collections import defaultdict
list1 = [['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']]
# list2 = [['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'],
list2 = [['tracking_code', '29285908', 'from_country', 'FR', 'to_country', 'FR', 'package_type_id', '10', 'transaction_id', '224847627', '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']]
list3 = [['tracking_code', '21703238', 'from_country', 'FR', 'to_country', 'FR', 'amount', '3.23'],
['tracking_code', '41545695', 'from_country', 'FR', 'to_country', 'FR', 'amount', '2.9']]
def aggregate_lists(*lists):
transactions = defaultdict(dict)
for list in lists:
for row in list:
try:
id_col = row.index('transaction_id')
transaction_id = row[id_col + 1]
except ValueError:
continue # Better error handling to be added.
for col in range(0, len(row), 2):
if col != id_col:
transactions[transaction_id][row[col]] = row[col + 1]
return transactions
def main():
transactions = aggregate_lists(list1, list2, list3)
for k, props in transactions.items():
print(f'Transaction: {k}')
for k, v in props.items():
print(f'\t{k}: {v}')
if __name__ == '__main__':
main()
Transaction: 224847627
product_amount: 2.73
user_invoice_date: 2018-12-21
tracking_code: 29285908
from_country: FR
to_country: FR
package_type_id: 10
shipping_label_created: 2018-09-25 18
40: 52
Transaction: 67919397
product_amount: 2.73
user_invoice_date: 2017-10-26
Transaction: 111423825
tracking_code: 22105784
from_country: FR
to_country: FR
package_type_id: 10
shipping_label_created: 2018-04-13 11
22: 44