访问Python中的嵌套字典以在for循环中进行比较
我正在尝试访问嵌套字典值以与另一个字典的键值进行比较。我有两本字典。如下所示:访问Python中的嵌套字典以在for循环中进行比较,python,Python,我正在尝试访问嵌套字典值以与另一个字典的键值进行比较。我有两本字典。如下所示: params = [{'nUID': '39', 'Query': [{'MaxRecords': '40', 'OrderName': 'Forecast Placeholder - 1005', 'CustomerID': '15283'}]}] response = [{ 'OrderID': 1028, 'Name': 'Forecast Placeholder - 1005', '
params = [{'nUID': '39', 'Query': [{'MaxRecords': '40', 'OrderName': 'Forecast Placeholder - 1005', 'CustomerID': '15283'}]}]
response = [{
'OrderID': 1028,
'Name': 'Forecast Placeholder - 1005',
'CustomerID': 1127}]
我需要比较params中的OrderName
值和response中的Name
。以下是我尝试过的:
adpoint_key = "AdPoint_Id__c"
for index, my_dict in enumerate(test_data):
if my_dict['DBU_Key__c'] != "AUT" and my_dict["AdPoint_Sync_Conditions__c"] != '"' and adpoint_key in my_dict.keys():
my_dict["OpportunityID"] = my_dict["AdPoint_Id__c"]
my_dict["CustomerID"] = my_dict["AdPoint_Customer_Id__c"]
params = []
params_dict = {}
params_dict["nUID"] = "39"
params_dict["Query"] = []
new_dict = {}
new_dict["MaxRecords"] = "40"
new_dict["OrderName"] = "Forecast Placeholder - " + my_dict["OpportunityID"]
new_dict["CustomerID"] = my_dict["CustomerID"]
params_dict["Query"].append(new_dict)
params.append(params_dict)
# print(params)
response = client.service.GetOrders(**params[0]))
results = []
for a, b in zip(params, response):
if b[0]['Name'] == a['Query'][0]['OrderName']:
results.append(response_dict)
print(results)
错误:
TypeError:字符串索引必须是整数
代码中有两个错误:
params
也是一个列表,因此也应该对其进行索引或枚举param[“query”]
的结果也是一个列表。您必须指定要访问列表中的哪些词典(尽管在本例中只有一个词典)for index, (response_dict, params_dict) in enumerate(zip(response, params)):
if response_dict["Name"] == params_dict["Query"][0]["OrderName"]:
results.append(response_dict)
print(results)
代码中有两个错误:
params
也是一个列表,因此也应该对其进行索引或枚举param[“query”]
的结果也是一个列表。您必须指定要访问列表中的哪些词典(尽管在本例中只有一个词典)for index, (response_dict, params_dict) in enumerate(zip(response, params)):
if response_dict["Name"] == params_dict["Query"][0]["OrderName"]:
results.append(response_dict)
print(results)
我怀疑问题在于
params
的实际结构与您访问它的方式不匹配
似乎params
是一个字典列表。然后,第一个dictionary对象包含一个名为Query
的键,其值也是字典列表。该列表中的第一个dictionary对象包含一个名为Order Name
的键,对应的值表示该名称
因此,给定您显示的params
中的示例数据,要检索订单名称
值预测占位符-1005
,您需要编写:
params[0][“查询”][0][“订单名称”]
首先检索外部列表中的第一个词典。然后检索名为
Query
然后检索由该值表示的列表中的第一个字典
然后检索名为
订单名称
如果这不是您想要的,那么您可能需要调整params
的结构
要获取您当前尝试使用的查询,需要如下所示:
params[“Query”][“OrderName”]
而params
需要如下所示:
params={'nUID':'39','Query':{'MaxRecords':'40','OrderName':'Forecast Placeholder-1005','CustomerID':'15283'}
您遇到的错误是,在访问外部列表时,您试图用字符串而不是整数索引到列表中,然后用括号表示如何使用它们,您试图用另一个字符串索引到字符串中
如果你需要更多的细节,请在评论中告诉我
编辑:
看起来这应该是你想要的:
results=[]
对于索引,枚举中的响应(响应):
如果响应\u dict[“Name”]==参数[0][“Query”][0][“OrderName”]:
结果。追加(应答)
打印(结果)
编辑:
由于您得到的是空引用错误,请尝试使用调试的“gravestones”技术
results=[]
对于索引,枚举中的响应(响应):
打印响应命令
打印响应内容[“名称”]
打印参数
打印参数[0]
打印参数[0][“查询”]
打印参数[0][“查询”][0]
打印参数[0][“查询”][0][“订单名称”]
如果响应\u dict[“Name”]==参数[0][“Query”][0][“OrderName”]:
结果。追加(应答)
打印(结果)
看看它在哪个打印语句上消失。另外,更完整的堆栈跟踪也非常有用我怀疑问题在于
params
的实际结构与您访问它的方式不匹配
似乎params
是一个字典列表。然后,第一个dictionary对象包含一个名为Query
的键,其值也是字典列表。该列表中的第一个dictionary对象包含一个名为Order Name
的键,对应的值表示该名称
因此,给定您显示的params
中的示例数据,要检索订单名称
值预测占位符-1005
,您需要编写:
params[0][“查询”][0][“订单名称”]
首先检索外部列表中的第一个词典。然后检索名为
Query
然后检索由该值表示的列表中的第一个字典
然后检索名为
订单名称
如果这不是您想要的,那么您可能需要调整params
的结构
要获取您当前尝试使用的查询,需要如下所示:
params[“Query”][“OrderName”]
而params
需要如下所示:
params={'nUID':'39','Query':{'MaxRecords':'40','OrderName':'Forecast Placeholder-1005','CustomerID':'15283'}
您遇到的错误是,在访问外部列表时,您试图用字符串而不是整数索引到列表中,然后用括号表示如何使用它们,您试图用另一个字符串索引到字符串中
如果你需要更多的细节,请在评论中告诉我
编辑:
看起来这应该是你想要的:
results=[]
对于索引,枚举中的响应(响应):
如果响应\u dict[“Name”]==参数[0][“Query”][0][“OrderName”]:
结果。追加(应答)
打印(结果)
编辑:
由于出现空引用错误,请尝试使用“gravestones”te
name_value = response[0]['Name']
for a, b in zip(params, response):
if b[0]['Name'] == a['Query'][0]['OrderName']:
# original data
params = [{'nUID': '39',
'Query': [{'MaxRecords': '40',
'OrderName': 'Forecast Placeholder - 1005',
'CustomerID': '15283'}]}]
response = [{
'OrderID': 1028,
'Name': 'Forecast Placeholder - 1005',
'CustomerID': 1127}]
results = list()
for p, r in zip(params, response):
# use .get() for dictionary look-ups, to to emphasize dict vs list access
if p.get('Query')[0].get('OrderName') == r.get('Name'):
results.append(r)
print(results)
[{'OrderID': 1028, 'Name': 'Forecast Placeholder - 1005', 'CustomerID': 1127}]