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

时间:2019-04-09 10:10:14

标签: python python-3.x dictionary

我有一些字典,其中一些值以列表的形式存储。我想组合两个键值并将该值分配给新键。如何合并两个键并将其分配给单个词典中的新键?

这是示例代码:

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",
    },
]

必填输出:

[
    {
        "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",
    },
]

1 个答案:

答案 0 :(得分:2)

这是一种方法。

例如:

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)

输出:

[{'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}]