I am having a problem counting the total number of unique ids in a nested list.
Nested list:
[
[
{
"id": "a",
"label": "Truck",
"annotation": "vehicle",
"visible": "No",
"label2": "Truck",
"shape": "rectangle",
"x": 4,
"y": 500,
"height": 200,
"width": 300
},
{
"id": "b",
"label": "Truck",
"annotation": "vehicle",
"visible": "No",
"label2": "Truck",
"shape": "rectangle",
"x": 3,
"y": 400,
"height": 250,
"width": 360
},
...
],
[
{
"id": "a",
"label": "Truck",
"annotation": "vehicle",
"visible": "No",
"label2": "Truck",
"shape": "rectangle",
"x": 4,
"y": 500,
"height": 200,
"width": 300
},
{
"id": "b",
"label": "Truck",
"annotation": "vehicle",
"visible": "No",
"label2": "Truck",
"shape": "rectangle",
"x": 3,
"y": 400,
"height": 250,
"width": 360
},
...
],
...
]
Currently, it keeps on printing out the result below, which is not what I want:
id: 1,
label: 1,
annotation: 1,
visible: 1,
label2: 1,
shape: 1,
x: 1,
y: 1,
height: 1,
width: 1
...
id: 1,
label: 1,
annotation: 1,
visible: 1,
label2: 1,
shape: 1,
x: 1,
y: 1,
height: 1,
width: 1
How can I get this nested list which also contains dictionaries to just count id "a" and "b" once without using pandas?
Output I do want:
Unique id: 2
Code:
import json
import os
import pandas as pd
from itertools import chain
path = 'mypath/json_name.json'
size = os.path.getsize(path)
def func1(data):
c = {}
for key,value in data.items():
try:
c[key].append(value)
except KeyError:
c[key] = [value]
for key,value in c.items():
print("{0}:{1}". format(key, len(set(value))))
def totalUniqueId(data):
for inner_list in data:
for inner_dict in inner_list:
func1(inner_dict)
with open('json_name.json') as json_file:
if size> 13000:
json_file.seek(0)
test_data = json.load(json_file)
totalUniqueId(test_data)
Resources I used: