英文:
combine a lists of nested with dictionaries
问题
我尝试将名为Customer Details的列表与名为Address、Credit card info和Vehicle的其他列表组合在一起,使用Customer Details列表中的"First Name"和"Last Name"。
我希望获得以下结果:
- [
{
"First Name": "Kieran",
"Last Name": "Wilson",
"Age (Years)": "89",
"Marital Status": "married or civil partner",
"Distance Commuted to Work (miles)": "0",
"Employer Company": "N/A",
"Dependants": "3",
"Yearly Pension (GBP)": "7257",
"Sex": "Male",
"Retired": "TRUE",
"Yearly Salary (GBP)": "72838"
},
{
"Address Street": "70 Lydia isle",
"Address City": "Lake Conor",
"Address Postcode": "S71 7XZ"
},
{
"Credit Card Start Date": "18-Aug",
"Credit Card Expiry Date": "27-Nov",
"Credit Card CVV": "875",
"Credit Card Number": "6.76374E+11",
"Bank IBAN": "GB62PQKB71416034141571"
},
{
"Vehicle Make": "Hyundai",
"Vehicle Model": "Bonneville",
"Vehicle Type": "Pickup",
"Vehicle Year": "2009"
}
]
对于第一个客户
英文:
I am trying to combine a list called Customer Details
- [{'First Name': 'Kieran', 'Last Name': 'Wilson', 'Age (Years)': '89', 'Marital Status': 'married or civil partner', 'Distance Commuted to Work (miles)': '0', 'Employer Company': 'N/A', 'Dependants': '3', 'Yearly Pension (GBP)': '7257', 'Sex': 'Male', 'Retired': 'TRUE', 'Yearly Salary (GBP)': '72838'}
with a list called Address - [{'Address Street': '70 Lydia isle', 'Address City': 'Lake Conor', 'Address Postcode': 'S71 7XZ'}, {'Address Street': '00 Wheeler wells', 'Address City': 'Chapmanton', 'Address Postcode': 'L2 7BT'}
and Credit card info - [{'Credit Card Start Date': '18-Aug', 'Credit Card Expiry Date': '27-Nov', 'Credit Card CVV': '875', 'Credit Card Number': '6.76374E+11', 'Bank IBAN': 'GB62PQKB71416034141571'}
and Vehicle - [{'Vehicle Make': 'Hyundai', 'Vehicle Model': 'Bonneville', 'Vehicle Type': 'Pickup', 'Vehicle Year': '2009'}
How do I combine this using First Name and Last Name in the Customer Details list
This is the result I am hoping to get
- [
{
"First Name": "Kieran",
"Last Name": "Wilson",
"Age (Years)": "89",
"Marital Status": "married or civil partner",
"Distance Commuted to Work (miles)": "0",
"Employer Company": "N/A",
"Dependants": "3",
"Yearly Pension (GBP)": "7257",
"Sex": "Male",
"Retired": "TRUE",
"Yearly Salary (GBP)": "72838"},
{
"Address Street": "70 Lydia isle",
"Address City": "Lake Conor",
"Address Postcode": "S71 7XZ"
},
{
"Credit Card Start Date": "18-Aug",
"Credit Card Expiry Date": "27-Nov",
"Credit Card CVV": "875",
"Credit Card Number": "6.76374E+11",
"Bank IBAN": "GB62PQKB71416034141571"
},
{
"Vehicle Make": "Hyundai",
"Vehicle Model": "Bonneville",
"Vehicle Type": "Pickup",
"Vehicle Year": "2009"
}
]
for the first customer
答案1
得分: 0
[[{'First Name': 'Kieran', 'Last Name': 'Wilson', 'Age (Years)': '89', 'Marital Status': 'married or civil partner', 'Distance Commuted to Work (miles)': '0', 'Employer Company': 'N/A', 'Dependants': '3', 'Yearly Pension (GBP)': '7257', 'Sex': 'Male', 'Retired': 'TRUE', 'Yearly Salary (GBP)': '72838'}, {'Address Street': '70 Lydia isle', 'Address City': 'Lake Conor', 'Address Postcode': 'S71 7XZ'}, {'Credit Card Start Date': '18-Aug', 'Credit Card Expiry Date': '27-Nov', 'Credit Card CVV': '875', 'Credit Card Number': '6.76374E+11', 'Bank IBAN': 'GB62PQKB71416034141571'}, {'Vehicle Make': 'Hyundai', 'Vehicle Model': 'Bonneville', 'Vehicle Type': 'Pickup', 'Vehicle Year': '2009'}]]
英文:
customer_details = [{'First Name': 'Kieran', 'Last Name': 'Wilson', 'Age (Years)': '89', 'Marital Status': 'married or civil partner', 'Distance Commuted to Work (miles)': '0', 'Employer Company': 'N/A', 'Dependants': '3', 'Yearly Pension (GBP)': '7257', 'Sex': 'Male', 'Retired': 'TRUE', 'Yearly Salary (GBP)': '72838'}]
address = [{'Address Street': '70 Lydia isle', 'Address City': 'Lake Conor', 'Address Postcode': 'S71 7XZ'}, {'Address Street': '00 Wheeler wells', 'Address City': 'Chapmanton', 'Address Postcode': 'L2 7BT'}]
cc_info = [{'Credit Card Start Date': '18-Aug', 'Credit Card Expiry Date': '27-Nov', 'Credit Card CVV': '875', 'Credit Card Number': '6.76374E+11', 'Bank IBAN': 'GB62PQKB71416034141571'}]
vehicle = [{'Vehicle Make': 'Hyundai', 'Vehicle Model': 'Bonneville', 'Vehicle Type': 'Pickup', 'Vehicle Year': '2009'}]
result = [[customer_details[i], address[i], cc_info[i], vehicle[i]] for i in range(len(customer_details))]
print(result)
results in:
[[{'First Name': 'Kieran', 'Last Name': 'Wilson', 'Age (Years)': '89', 'Marital Status': 'married or civil partner', 'Distance Commuted to Work (miles)': '0', 'Employer Company': 'N/A', 'Dependants': '3', 'Yearly Pension (GBP)': '7257', 'Sex': 'Male', 'Retired': 'TRUE', 'Yearly Salary (GBP)': '72838'}, {'Address Street': '70 Lydia isle', 'Address City': 'Lake Conor', 'Address Postcode': 'S71 7XZ'}, {'Credit Card Start Date': '18-Aug', 'Credit Card Expiry Date': '27-Nov', 'Credit Card CVV': '875', 'Credit Card Number': '6.76374E+11', 'Bank IBAN': 'GB62PQKB71416034141571'}, {'Vehicle Make': 'Hyundai', 'Vehicle Model': 'Bonneville', 'Vehicle Type': 'Pickup', 'Vehicle Year': '2009'}]]
答案2
得分: 0
你可以使用字典合并来实现这一点。
字典合并运算符 **
是自 Python 3.9 版本引入的。请注意有两个不同的合并运算符,|
和 **
。|
运算符用于创建联合,可以消除重复的键。
customer_details = [{'First Name': 'Kieran', 'Last Name': 'Wilson', 'Age (Years)': '89', 'Marital Status': 'married or civil partner', 'Distance Commuted to Work (miles)': '0', 'Employer Company': 'N/A', 'Dependants': '3', 'Yearly Pension (GBP)': '7257', 'Sex': 'Male', 'Retired': 'TRUE', 'Yearly Salary (GBP)': '72838'}]
address = [{'Address Street': '70 Lydia isle', 'Address City': 'Lake Conor', 'Address Postcode': 'S71 7XZ'}, {'Address Street': '00 Wheeler wells', 'Address City': 'Chapmanton', 'Address Postcode': 'L2 7BT'}]
credit_card_info = [{'Credit Card Start Date': '18-Aug', 'Credit Card Expiry Date': '27-Nov', 'Credit Card CVV': '875', 'Credit Card Number': '6.76374E+11', 'Bank IBAN': 'GB62PQKB71416034141571'}]
vehicle = [{'Vehicle Make': 'Hyundai', 'Vehicle Model': 'Bonneville', 'Vehicle Type': 'Pickup', 'Vehicle Year': '2009'}]
# 基于客户的名和姓合并所有列表
result = [{**customer, **{'Address': address, 'Credit Card Info': credit_card_info, 'Vehicle': vehicle}} for customer in customer_details]
print(result)
英文:
You can use dictionary merging to achieve this.
Dictionary merge operator **
is new since Python 3.9. Be aware there are two different merge-operators, |
and **
. The |
operator creates unions, which eliminates duplicate keys.
customer_details = [{'First Name': 'Kieran', 'Last Name': 'Wilson', 'Age (Years)': '89', 'Marital Status': 'married or civil partner', 'Distance Commuted to Work (miles)': '0', 'Employer Company': 'N/A', 'Dependants': '3', 'Yearly Pension (GBP)': '7257', 'Sex': 'Male', 'Retired': 'TRUE', 'Yearly Salary (GBP)': '72838'}]
address = [{'Address Street': '70 Lydia isle', 'Address City': 'Lake Conor', 'Address Postcode': 'S71 7XZ'}, {'Address Street': '00 Wheeler wells', 'Address City': 'Chapmanton', 'Address Postcode': 'L2 7BT'}]
credit_card_info = [{'Credit Card Start Date': '18-Aug', 'Credit Card Expiry Date': '27-Nov', 'Credit Card CVV': '875', 'Credit Card Number': '6.76374E+11', 'Bank IBAN': 'GB62PQKB71416034141571'}]
vehicle = [{'Vehicle Make': 'Hyundai', 'Vehicle Model': 'Bonneville', 'Vehicle Type': 'Pickup', 'Vehicle Year': '2009'}]
# Merge all the lists based on the customer's first name and last name
result = [{**customer, **{'Address': address, 'Credit Card Info': credit_card_info, 'Vehicle': vehicle}} for customer in customer_details]
print(result)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论