【问题标题】:Merge two dictionaries with an identical key using foor loop使用 for 循环合并两个具有相同键的字典
【发布时间】:2021-07-19 00:15:18
【问题描述】:

我有这两个字典,我想在 dict2 中添加数据 dict1。我如何用 python 做到这一点?

dict1 = {
    213688169:
        {'stationCode': '6003',
         'station_id': 213688169,
         'num_bikes_available': 10,
         'numBikesAvailable': 10,
         'num_bikes_available_types': [{'mechanical': 10}, {'ebike': 0}],
         'num_docks_available': 10,
         'numDocksAvailable': 10,
         'is_installed': 1,
         'is_returning': 1,
         'is_renting': 1,
         'last_reported': 1619207955}
}

dict2 = {
    213688169:
        {'station_id': 213688169,
         'name': 'Benjamin Godard - Victor Hugo',
         'lat': 48.865983,
         'lon': 2.275725,
         'capacity': 35,
         'stationCode': '16107'}
}

我试过了,但是太长太复杂了:

donnees=[]
for i in stations:
    for j in velib_description :
         if i['station_id'] == j['station_id']:
                List={}
                List['name'] = i['name']
                List['lat'] = i['lat']
                List['lon'] = i['lon']
                List['capacity'] = i['capacity']
                List['num_bikes_available'] = j['num_bikes_available']
                List['num_bikes_available_types'] = j['num_bikes_available_types']
                List['last_reported'] = j['last_reported']
                donnees.append(List)

我要加dict_2 = {num_bikes_available', 'num_bikes_available_types', last_reported': 1619207955 }

谢谢

【问题讨论】:

  • “我试过了,但它太长而且太复杂了”——它也不起作用,即 station 和 velib_description 是未定义的。

标签: python json dictionary merge add


【解决方案1】:

也许你想要dict.update

dict1 = {
    213688169: {
        "stationCode": "6003",
        "station_id": 213688169,
        "num_bikes_available": 10,
        "numBikesAvailable": 10,
        "num_bikes_available_types": [{"mechanical": 10}, {"ebike": 0}],
        "num_docks_available": 10,
        "numDocksAvailable": 10,
        "is_installed": 1,
        "is_returning": 1,
        "is_renting": 1,
        "last_reported": 1619207955,
    }
}

dict2 = {
    213688169: {
        "station_id": 213688169,
        "name": "Benjamin Godard - Victor Hugo",
        "lat": 48.865983,
        "lon": 2.275725,
        "capacity": 35,
        "stationCode": "16107",
    }
}

dict1_item = dict1[213688169]
dict2[213688169].update({k: v for k, v in dict1_item.items() if k not in dict2})

dict2

打印出来

{213688169: {'station_id': 213688169,
  'name': 'Benjamin Godard - Victor Hugo',
  'lat': 48.865983,
  'lon': 2.275725,
  'capacity': 35,
  'stationCode': '6003',
  'num_bikes_available': 10,
  'numBikesAvailable': 10,
  'num_bikes_available_types': [{'mechanical': 10}, {'ebike': 0}],
  'num_docks_available': 10,
  'numDocksAvailable': 10,
  'is_installed': 1,
  'is_returning': 1,
  'is_renting': 1,
  'last_reported': 1619207955}}

我们使用推导从第一个字典中获取不在第二个字典中的所有键/值,然后更新第二个字典。我们必须对 213688169 键进行一些修改,因为您的字典是嵌套的。

【讨论】:

    【解决方案2】:

    您可以使用带有dictionary merge trick 的字典推导:

    dict3 = {k: {**v, **dict2[k]} for k, v in dict1.items()}
    

    或者对于 Python 3.9 及更高版本:

    dict3 = {k: v | dict2[k] for k, v in dict1.items()}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      相关资源
      最近更新 更多