将嵌套的列表与字典组合在一起。

huangapple go评论72阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年3月9日 19:51:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定