简体   繁体   English

合并两个键值并将其分配给单个字典中的新键

[英]Merge two key values and assign it to a new key in a single dictionary

I have dictionary with some values stored in the form of list.我有一些以列表形式存储的值的字典。 I want to combine two key values and assign the value to the new key.我想组合两个键值并将值分配给新键。 How to merge two keys and assign it to a new key in a single dictionary?如何合并两个键并将其分配给单个字典中的新键?

Here's the sample code:这是示例代码:

fields = ["Classification", "Fuel_Type"]  # two fields to combine
target = "Classification_Fuel_Type"
d = [
    {
        "Fuel": "Gas",
        "Gears": 6,
        "Width": 209,
        "Year": 2012,
        "Engine": "Lincoln 5.4L 8 Cylinder 310 hp 365 ft-lbs FFV",
        "Classification": "Automatic transmission",
    },
    {
        "Fuel": "E85",
        "Gears": 5,
        "Width": 209,
        "Year": 2014,
        "Engine": "Lincoln 5.4L 8 Cylinder 310 hp 365 ft-lbs FFV",
        "Classification": "Automatic transmission",
    },
    {
        "Fuel": "E85",
        "Gears": 6,
        "Width": 509,
        "Year": 2011,
        "Engine": "Lincoln 5.4L 8 Cylinder 310 hp 365 ft-lbs FFV",
        "Classification": "Automatic transmission",
    },
]

Required output:所需输出:

[
    {
        "Classification_Fuel_Type": "Automatic transmissionGas",
        "Fuel": "Gas",
        "Gears": 6,
        "Width": 209,
        "Year": 2012,
        "Engine": "Lincoln 5.4L 8 Cylinder 310 hp 365 ft-lbs FFV",
        "Classification": "Automatic transmission",
    },
    {
        "Classification_Fuel_Type": "Automatic transmissionE85",
        "Fuel": "E85",
        "Gears": 5,
        "Width": 209,
        "Year": 2014,
        "Engine": "Lincoln 5.4L 8 Cylinder 310 hp 365 ft-lbs FFV",
        "Classification": "Automatic transmission",
    },
    {
        "Classification_Fuel_Type": "Automatic transmissionE85",
        "Fuel": "E85",
        "Gears": 6,
        "Width": 509,
        "Year": 2011,
        "Engine": "Lincoln 5.4L 8 Cylinder 310 hp 365 ft-lbs FFV",
        "Classification": "Automatic transmission",
    },
]

This is one approach.这是一种方法。

Ex:前任:

fields = ["Classification","Fuel"]  #I believe Fuel_Type is a typo...
target = "Classification_Fuel_Type"
d = [
{'Fuel': 'Gas', 'Gears': 6, 'Width': 209, 'Year': 2012, 'Engine': 'Lincoln 5.4L 8 Cylinder 310 hp 365 ft-lbs FFV', 'Classification': 'Automatic transmission'}, 
{'Fuel': 'E85', 'Gears': 5, 'Width': 209, 'Year': 2014, 'Engine': 'Lincoln 5.4L 8 Cylinder 310 hp 365 ft-lbs FFV', 'Classification': 'Automatic transmission'}, 
{'Fuel': 'E85', 'Gears': 6, 'Width': 509, 'Year': 2011, 'Engine': 'Lincoln 5.4L 8 Cylinder 310 hp 365 ft-lbs FFV', 'Classification': 'Automatic transmission'}]

for i in d:
    i[target] = "".join(i[k] for k in fields)

print(d)

Output:输出:

[{'Classification': 'Automatic transmission',
  'Classification_Fuel_Type': 'Automatic transmissionGas',
  'Engine': 'Lincoln 5.4L 8 Cylinder 310 hp 365 ft-lbs FFV',
  'Fuel': 'Gas',
  'Gears': 6,
  'Width': 209,
  'Year': 2012},
 {'Classification': 'Automatic transmission',
  'Classification_Fuel_Type': 'Automatic transmissionE85',
  'Engine': 'Lincoln 5.4L 8 Cylinder 310 hp 365 ft-lbs FFV',
  'Fuel': 'E85',
  'Gears': 5,
  'Width': 209,
  'Year': 2014},
 {'Classification': 'Automatic transmission',
  'Classification_Fuel_Type': 'Automatic transmissionE85',
  'Engine': 'Lincoln 5.4L 8 Cylinder 310 hp 365 ft-lbs FFV',
  'Fuel': 'E85',
  'Gears': 6,
  'Width': 509,
  'Year': 2011}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM