简体   繁体   English

计算JSON文件中的唯一值

[英]Count unique values in JSON file

I have a JSON file with the following setup: 我有一个具有以下设置的JSON文件:

{
    "1": {
        "id":123456,
        "name":"John Doe",
        "address":"Address 1"
    }, 
    "2": {
        "id":789011,
        "name":"Name Nameson",
        "address":"Address 2"
    }
}

The thing with this file, is that it contains A LOT of duplicates. 该文件的作用是它包含许多重复项。 So what I want is to be able to count how many unique people there are just by their ID. 所以我想要的是能够仅通过其ID来计算有多少不重复的人。 Or really any value they have would work, as no one has the same name or address. 或实际上,他们拥有的任何价值都将奏效,因为没有人拥有相同的名称或地址。 So say there are 300 people in the list, and only 50 of them are unique, the rest are duplicates, I want to be able to display that. 如此说来,列表中有300个人,其中只有50个人是唯一的,其余都是重复的,我希望能够显示出来。

I've looked it up on google, but I can't really find anything that works for me... 我在Google上进行了查询,但是找不到适合我的任何东西...

You can add all the ids to a set: 您可以将所有ID添加到集合中:

unique_ids = set(item['id'] for item in my_data.values())
print(len(unique_ids))

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

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