Assuming you want a list of dicts as the output of the json with each dict be the form in your question:
The following one liner will put it into the data structure your looking for with each tuple generating it's own complete structure:
[{'name':i[0], 'children': [{'name': i[1], 'value': i[2]}]} for i in tuples]
But I suspect you want the outer name to be unique with inner children being built from multiple tuples, such that there is only one such structure generated with 'name' == X.
To do it in a memory efficient manner start by sorting your tuples by X:
# should work as long as you aren't doing any fancy sorting
stuples = sorted(tuples) name = None
dat = None
for t in stuples:if name != t[0]:if dat is not None:writeDat(json.dumps(dat))name = t[0]dat = {'name': t[0], 'children': [{'name': t[1], 'value': t[2]}]}else:dat['children'].append({'name': t1, 'value': t[2]})
I assume you have a function to write one of these out such as writeDat() that takes the json so you aren't storing them all in ram.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…
Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…
I was trying to learn Python when I came upon this question. I am not asking for you to answer the question for me, I just need some help. Note: I am only allowed to use loops and if statements etc. No…
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…
I wrote this codedef partE():e = 3 * 10 // 3 + 10 % 3print("e).", e)partE()and python comes back with this error message when I try to run it. I do not understand why. Can someone please expl…
I need to find the multiplicative digital root of a number in python using only loops.something that does the same as the below code but using loops:print("multiplicative digital root of a number …
I have a script that pulls json data from an api, and I want it to then after pulling said data, decode and pick which tags to store into a db. Right now I just need to get the script to return specifi…
I need a program which can make graphs by matplotlib with functions I write in the console.
But it doesnt work with trigonometric functions.
The code I already wrote is:from numpy import linspace
impo…
This question already has answers here:What do the symbols "=" and "==" mean in python? When is each used?(5 answers)Closed 5 years ago.I wonder know whats the difference between …