I have JSON File that has this format
{
"links": [
{"source":"0","target":"1","weight":1,"color":"white"},
{"source":"0","target":"2","weight":1,"color":"yellow"},
{"source":"0","target":"3","weight":1,"color":"white"},
]
}
I want to collect all target
for a single source
like this:
{"source": 0, "neighbors": ["1","2","3"]}
where neighbors
are all the collected target
Here's my Code
import json
with open("linksGr.json") as file:
data = json.load(file)
collectDict = {}
for obj in data["links"]:
if (collectDict["source"] == obj["source"]):
collectDict["neighbour"] = obj["target"]
I just need a way to accumulate all targets for each source instead of there being multiple sources as I have done here
collectDict["source"] = obj["source"]
collectDict["neighbour"] = obj["target"]
Any help would be appreciated a lot. I am sure there is some basic concept and a simple way that I am missing here. Thanks for the help.
If I understand you correctly, you can use collections.defaultdict
, to map from source to a list of targets, like this:
(I added some data to have multiple sources)
from collections import defaultdict
data = {
"links": [
{"source":"0","target":"1","weight":1,"color":"white"},
{"source":"0","target":"2","weight":1,"color":"yellow"},
{"source":"0","target":"3","weight":1,"color":"white"},
{"source":"5","target":"7","weight":1,"color":"white"},
{"source":"5","target":"8","weight":1,"color":"yellow"},
{"source":"6","target":"9","weight":1,"color":"white"},
]
}
collectDict = defaultdict(list)
for obj in data["links"]:
collectDict[obj["source"]].append(obj["target"])
print(dict(collectDict))
Output:
{'0': ['1', '2', '3'], '5': ['7', '8'], '6': ['9']}
EDIT: Here's another method using itertools.groupby
, assuming the links are ordered by sources (otherwise, just sort it before)
from itertools import groupby
collectDict = {k: [t["target"] for t in g] for k,g in groupby(data["links"], lambda obj: obj["source"])}
print(collectDict)