使用循环和字典理解将客户的 ID 替换为装运字典中各自的名称
replace client’s id with their respective name in shipment dictionary using a loop and dictionary comprehension
d1={101:{'Sender':1,'Receiver':3,'Start date':'14-03-2020','Delivery date':'25- 03-2020','Sender location':'Area 1','Receiver location':'Area 6','Delivery status':'Delivered','Shipping cost':198},
102:{'Sender':4,'Receiver':1,'Start date':'18-06-2020','Delivery date':'09-07-2020','Sender location':'Area 2','Receiver location':'Area 4','Delivery status':'Delivered','Shipping cost':275},
103:{'Sender':2,'Receiver':3,'Start date':'01-12-2020','Delivery date':'Null','Sender location' :'Area 5','Receiver location':'Area 1','Delivery status':'In Transit','Shipping cost':200},
104:{'Sender':1,'Receiver':5,'Start date':'23-06-2020','Delivery date':'25-06-2020','Sender location':'Area 1','Receiver location':'Area 4','Delivery status':'Delivered','Shipping cost':314},
105:{'Sender':3,'Receiver':4,'Start date':'29-08-2020','Delivery date':'10-09-2020','Sender location':'Area 5','Receiver location':'Area 3','Delivery status':'Delivered','Shipping cost':275},
106:{'Sender':5,'Receiver':2,'Start date':'28-06-2020','Delivery date':'Null','Sender location' :'Area 3','Receiver location':'Area 1','Delivery status':'In Transit','Shipping cost':270}}
d2 = {1:'Phillip',2:'Omega lll',3:'Ramya',4:'Romesh',5:'John'}
您可以遍历第一个字典并用第二个字典中的名称替换 id,如果该 id 在第二个字典中:
d1={101:{'Sender':1,'Receiver':3,'Start date':'14-03-2020','Delivery date':'25-03-2020','Sender location':'Area 1','Receiver location':'Area 6','Delivery status':'Delivered','Shipping cost':198}, 102:{'Sender':4,'Receiver':1,'Start date':'18-06-2020','Delivery date':'09-07-2020','Sender location':'Area 2','Receiver location':'Area 4','Delivery status':'Delivered','Shipping cost':275}, 103:{'Sender':2,'Receiver':3,'Start date':'01-12-2020','Delivery date':'Null','Sender location':'Area 5','Receiver location':'Area 1','Delivery status':'In Transit','Shipping cost':200}, 104:{'Sender':1,'Receiver':5,'Start date':'23-06-2020','Delivery date':'25-06-2020','Sender location':'Area 1','Receiver location':'Area 4','Delivery status':'Delivered','Shipping cost':314}, 105:{'Sender':3,'Receiver':4,'Start date':'29-08-2020','Delivery date':'10-09-2020','Sender location':'Area 5','Receiver location':'Area 3','Delivery status':'Delivered','Shipping cost':275}, 106:{'Sender':5,'Receiver':2,'Start date':'28-06-2020','Delivery date':'Null','Sender location':'Area 3','Receiver location':'Area 1','Delivery status':'In Transit','Shipping cost':270}}
d2 = {1:'Phillip',2:'Omega lll',3 :'Ramya',4:'Romesh',5:'John'}
result = {}
for key, value in d1.items():
new_value = {k: d2[v] if k in ('Sender', 'Receiver') else v for k,v in value.items()}
result[key] = new_value
print(result)
或者,你也可以使用字典理解来计算这一切:
result = {key:{k: d2[v] if k in ('Sender', 'Receiver') else v for k,v in value.items()} for key, value in d1.items()}
你可以使用 enumerate()
ship_info_alt = {k:[client_data[v] if ind in (0, 1) else v for ind, v in enumerate(v)] for k, v in ship_info.items()}
d1={101:{'Sender':1,'Receiver':3,'Start date':'14-03-2020','Delivery date':'25- 03-2020','Sender location':'Area 1','Receiver location':'Area 6','Delivery status':'Delivered','Shipping cost':198}, 102:{'Sender':4,'Receiver':1,'Start date':'18-06-2020','Delivery date':'09-07-2020','Sender location':'Area 2','Receiver location':'Area 4','Delivery status':'Delivered','Shipping cost':275}, 103:{'Sender':2,'Receiver':3,'Start date':'01-12-2020','Delivery date':'Null','Sender location' :'Area 5','Receiver location':'Area 1','Delivery status':'In Transit','Shipping cost':200}, 104:{'Sender':1,'Receiver':5,'Start date':'23-06-2020','Delivery date':'25-06-2020','Sender location':'Area 1','Receiver location':'Area 4','Delivery status':'Delivered','Shipping cost':314}, 105:{'Sender':3,'Receiver':4,'Start date':'29-08-2020','Delivery date':'10-09-2020','Sender location':'Area 5','Receiver location':'Area 3','Delivery status':'Delivered','Shipping cost':275}, 106:{'Sender':5,'Receiver':2,'Start date':'28-06-2020','Delivery date':'Null','Sender location' :'Area 3','Receiver location':'Area 1','Delivery status':'In Transit','Shipping cost':270}}
d2 = {1:'Phillip',2:'Omega lll',3:'Ramya',4:'Romesh',5:'John'}
您可以遍历第一个字典并用第二个字典中的名称替换 id,如果该 id 在第二个字典中:
d1={101:{'Sender':1,'Receiver':3,'Start date':'14-03-2020','Delivery date':'25-03-2020','Sender location':'Area 1','Receiver location':'Area 6','Delivery status':'Delivered','Shipping cost':198}, 102:{'Sender':4,'Receiver':1,'Start date':'18-06-2020','Delivery date':'09-07-2020','Sender location':'Area 2','Receiver location':'Area 4','Delivery status':'Delivered','Shipping cost':275}, 103:{'Sender':2,'Receiver':3,'Start date':'01-12-2020','Delivery date':'Null','Sender location':'Area 5','Receiver location':'Area 1','Delivery status':'In Transit','Shipping cost':200}, 104:{'Sender':1,'Receiver':5,'Start date':'23-06-2020','Delivery date':'25-06-2020','Sender location':'Area 1','Receiver location':'Area 4','Delivery status':'Delivered','Shipping cost':314}, 105:{'Sender':3,'Receiver':4,'Start date':'29-08-2020','Delivery date':'10-09-2020','Sender location':'Area 5','Receiver location':'Area 3','Delivery status':'Delivered','Shipping cost':275}, 106:{'Sender':5,'Receiver':2,'Start date':'28-06-2020','Delivery date':'Null','Sender location':'Area 3','Receiver location':'Area 1','Delivery status':'In Transit','Shipping cost':270}}
d2 = {1:'Phillip',2:'Omega lll',3 :'Ramya',4:'Romesh',5:'John'}
result = {}
for key, value in d1.items():
new_value = {k: d2[v] if k in ('Sender', 'Receiver') else v for k,v in value.items()}
result[key] = new_value
print(result)
或者,你也可以使用字典理解来计算这一切:
result = {key:{k: d2[v] if k in ('Sender', 'Receiver') else v for k,v in value.items()} for key, value in d1.items()}
你可以使用 enumerate()
ship_info_alt = {k:[client_data[v] if ind in (0, 1) else v for ind, v in enumerate(v)] for k, v in ship_info.items()}