Hoping this title is not too unclear, I'd like to add do a nested dictionary comprehension, with the underlying goal of adding node attributes i NetworkX using loops, with output similar to this:
[('x1', {'a': 0}, {'b': 5}, {'c': 10} ),
('x2', {'a': 1}, {'b': 6}, {'c': 11} ),
...]
What I thought might work:
a = [ ( 'x%d' % h, {'1st': i}, {'2nd': j}, {'3rd': k} )
for h in range(1,17), for i in range(0,6),
for j in range(5,11), for k in range(10,16) ]
But this returned "SyntaxError: invalid syntax".
EDIT: I will ask the previously mentioned second part in a separate question - thanks so far!
There are syntax errors in your code: the commas before for
are invalid. You just need to remove them as mentioned in the previous answer. However, it will behave like nested for
loops, while you want h, i, j, k
to increase simultaneously. The desired output could be achieved as follows:
[ ( 'x%d' % h, {'1st': i}, {'2nd': j}, {'3rd': k} )
for h, i, j, k in zip(range(1,17), range(0,6), range(5,11), range(10,16))]
Or even better:
[ ( 'x%d' % (i+1), {'1st': i}, {'2nd': i+5}, {'3rd': i+10} ) for i in range(6)]