如何比较对象列表并只保留_new_对象?

How to compare list of objects and keep only _new_ objects?

我有两个 JSON 文件,分别名为 newold,其中包含一些数据。在这里我想比较 new.jsonold.json 文件,同时比较这两个 JSON 文件中是否有相同的数据 我不想创建任何新的 JSON 文件

如果我在 new.jsonold.json

中有如下不同的数据

new.json:

[
 {
    "name": "Mohan raj",
    "age": 23,
    "country": "INDIA"
 },
 {
    "name": "Kiruthika",
    "age": 18,
    "country": "INDIA"
 },
 {
    "name": "Munusamy",
    "age": 45,
    "country": "INDIA"
 },
 {
    "name": "John Wood",
    "age": 35,
    "country": "USA"
 },
 {
    "name": "Mark Smith",
    "age": 25,
    "country": "USA"
 }
]

old.json:

[
 {
    "name": "John Wood",
    "age": 35,
    "country": "USA"
 },
 {
    "name": "Mark Smith",
    "age": 30,
    "country": "USA"
 },
 {
    "name": "Oscar Bernard",
    "age": 25,
    "country": "Australia"
 }
]

如果 new.json 文件具有 old.json 的任何相同数据,我们必须跳过该数据并且 new.json 文件具有 [=16] 的任何更新数据=] 有了 new.json 中的新数据,我们必须使用上述场景的数据创建一个名为 updated.json 的新 JSON 文件。

生成的 JSON 文件需要如下所示:

updated.json:

[
 {
    "name": "Mohan raj",
    "age": 23,
    "country": "INDIA"
 },
 {
    "name": "Kiruthika",
    "age": 18,
    "country": "INDIA"
 },
 {
    "name": "Munusamy",
    "age": 45,
    "country": "INDIA"
 },
 {
    "name": "Mark Smith",
    "age": 25,
    "country": "USA"
 }
]

您可以在下面实现。跟踪旧 json.

的数据
import json

# read json file
with open('new.json') as f:
    new_data = json.load(f)
with open('old.json') as f:
    old_data = json.load(f)

old_json_list = [
    {elem["name"], elem["age"], elem["country"]} for elem in old_data]
    
updated_list = []
for elem in new_data:
    elm = {elem["name"], elem["age"], elem["country"]}
    if elm not in old_json_list:
        updated_list.append(elem)

with open('updated.json', 'w') as f:
    json.dump(updated_list, f)

费了点功夫,谢谢你回答我的问题,好像“updated”可以简单地表达为“new not in old”?

我认为是的,因为以下似乎可以完成这项工作。

关键是对对象本身进行比较,不想进入对象比较(deep-equal),只是散列每个对象返回JSON 给了我们可以比较的字符串表示形式:

import json

old_hashes = []

old_objs = json.load(open('old.json'))
for old_obj in old_objs:
    old_hash = json.dumps(old_obj)
    old_hashes.append(old_hash)


# "Updated" means "new not in old"
updated_objs = []

new_objs = json.load(open('new.json'))
for new_obj in new_objs:
    new_hash = json.dumps(new_obj)
    if new_hash not in old_hashes:
        updated_objs.append(new_obj)


print(json.dumps(updated_objs, indent=2))

当我 运行 反对你的 old.jsonnew.json 时,我得到:

[
  {
    "name": "Mohan raj",
    "age": 23,
    "country": "INDIA"
  },
  {
    "name": "Kiruthika",
    "age": 18,
    "country": "INDIA"
  },
  {
    "name": "Munusamy",
    "age": 45,
    "country": "INDIA"
  },
  {
    "name": "Mark Smith",
    "age": 25,
    "country": "USA"
  }
]