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)
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"},
]
}
collected = []
for obj in data["links"]:
source_matches = [item for item in collected if item["source"] == obj["source"]]
if len(source_matches) == 0:
source_match = {"source": obj["source"], "neighbour": [obj["target"]]}
collected.append(source_match)
elif len(source_matches) == 1:
source_matches[0]["neighbour"].append(obj["target"])
else:
raise BaseException()
print(collected) # [{'source': '0', 'neighbour': ['1', '2', '3']}, {'source': '5', 'neighbour': ['7', '8']}, {'source': '6', 'neighbour': ['9']}]
Not very elegant, but does the job.
If you don't really need the format {"source": 0, "neighbors": ["1","2","3"]} I recommend the above solution using defaultdict. If you need this format, you could also build it from the output of the defaultdict solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With