【问题标题】:How join/merge/update JSON dictionaries without overwriting data如何在不覆盖数据的情况下加入/合并/更新 JSON 字典
【发布时间】:2020-12-31 04:50:00
【问题描述】:

我有一个 JSON 字典列表,如下所示:

data = [{
    "title": "Bullitt",
    "release_year": "1968",
    "locations": "1153-57 Taylor Street",
    "fun_facts": "Embarcadero Freeway, which was featured in the film was demolished in 1989 because of structural damage from the 1989 Loma Prieta Earthquake)",
    "production_company": "Warner Brothers / Seven Arts\nSeven Arts",
    "distributor": "Warner Brothers",
    "director": "Peter Yates",
    "writer": "Alan R. Trustman",
    "actor_1": "Steve McQueen",
    "actor_2": "Jacqueline Bisset",
    "actor_3": "Robert Vaughn",
    "id": 498
},
{
    "title": "Bullitt",
    "release_year": "1968",
    "locations": "John Muir Drive (Lake Merced)",
    "production_company": "Warner Brothers / Seven Arts\nSeven Arts",
    "distributor": "Warner Brothers",
    "director": "Peter Yates",
    "writer": "Alan R. Trustman",
    "actor_1": "Steve McQueen",
    "actor_2": "Jacqueline Bisset",
    "actor_3": "Robert Vaughn",
    "id": 499
}]

如何在不覆盖数据的情况下合并这些字典?

所以,我想要得到的最终结果是:

data = {
    "title": "Bullitt",
    "release_year": "1968",
    "locations": ["1153-57 Taylor Street", "John Muir Drive (Lake Merced)"]
    "fun_facts": "Embarcadero Freeway, which was featured in the film was demolished in 1989 because of structural damage from the 1989 Loma Prieta Earthquake)",
    "production_company": "Warner Brothers / Seven Arts\nSeven Arts",
    "distributor": "Warner Brothers",
    "director": "Peter Yates",
    "writer": "Alan R. Trustman",
    "actor_1": "Steve McQueen",
    "actor_2": "Jacqueline Bisset",
    "actor_3": "Robert Vaughn",
    "id": 498, 499
}

我研究了合并 JSON 对象,但我遇到的只是覆盖数据。我不想覆盖任何东西。不太确定如何解决这个问题。

我是否必须为位置字段创建一个空列表并搜索整个数据集以查找相同的标题并获取它们的位置并将它们附加到空列表中,然后最后更新字典?或者有没有更好的方法/最佳实践?

【问题讨论】:

    标签: json python-3.x mergeddictionaries


    【解决方案1】:

    这是一种使用简单迭代的方法。

    例如:

    result = {}
    tolook = ('locations', 'id')
    for d in data:
        if d['title'] not in result:
            result[d['title']] = {k: [v] if k in tolook else v for k, v in d.items()}
        else:
            for i in tolook:
                result[d['title']][i].append(d[i])
    
    print(result) # Or result.values()
    

    输出:

    {'Bullitt': {'actor_1': 'Steve McQueen',
                 'actor_2': 'Jacqueline Bisset',
                 'actor_3': 'Robert Vaughn',
                 'director': 'Peter Yates',
                 'distributor': 'Warner Brothers',
                 'fun_facts': 'Embarcadero Freeway, which was featured in the film '
                              'was demolished in 1989 because of structural damage '
                              'from the 1989 Loma Prieta Earthquake)',
                 'id': [498, 499],
                 'locations': ['1153-57 Taylor Street',
                               'John Muir Drive (Lake Merced)'],
                 'production_company': 'Warner Brothers / Seven Arts\nSeven Arts',
                 'release_year': '1968',
                 'title': 'Bullitt',
                 'writer': 'Alan R. Trustman'}}
    

    【讨论】:

      【解决方案2】:
      python Dictionary
      -----------------
      Dictionaries store data values in key:value pairs. A collection which is unordered, changeable and does not allow duplicates.
      
      thisdict = {
        "brand": "Ford",
        "model": "Mustang",
        "year": 1964
      }
      
      
      python List
      -----------
      Lists are used to store multiple items in a single variable.
      We can change, add, and remove items in a list after it has been created.
      Since lists are indexed, lists can have items with the same value:
      
      mylist = ["apple", "banana", "cherry"]
      
      
      heres my logic, hope it helps.
      ------------------------------
      
      temp = {}
      
      for each dictionary in data[{},{}] { 
        for each key in dictionary.keys {
          does temp.keys contain key {  
            for each value in dictionary.key.values {
              does value exist in temp.key.values {
                # do nothing
              }
              else {add value to corresponding temp.key.values}
            }
          } else {(add key value pair)}
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-23
        • 1970-01-01
        • 2019-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-15
        相关资源
        最近更新 更多