0

I'm struggling to understand how to create variable lists in python (I'm an R programmer). I want to create a variable list name to store an array of minimum paths in a network with say 6 loads (sinks) and 3 generators (sources) using 'networkx', something like this: nx is the network and G is a Digraph

for i in sinks:
    print("Analysis for node ",i," :")
    for j in sources:
        for path in nx.all_simple_paths(G,j,i):
            print (path)
            "node_"+str(i)[i] = path #How to do this?? 

in such a way that "node_1" has all paths from node 1 to all sources, "node_2" all paths from node 2 to all sources, etc.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Augusto
  • 61
  • 7
  • 4
    Dynamic variables are considered a bad practice. It would be better to store those values in a list/dictionary. –  Nov 07 '13 at 00:54
  • See [Keep data out of your variable names](http://nedbatchelder.com/blog/201112/keep_data_out_of_your_variable_names.html) and [Why you don't want to dynamically create variables](http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html) and the answers on the 11 different SO questions linked from the second of those. – abarnert Nov 07 '13 at 01:53
  • possible duplicate of [dynamic variable](http://stackoverflow.com/questions/10963804/dynamic-variable) – abarnert Nov 07 '13 at 01:53

1 Answers1

2

Use a dictionary:

for i in sinks:
    print("Analysis for node ",i," :")
    for j in sources:
        for path in nx.all_simple_paths(G,j,i):
            print (path)
            node[i][j] = path
chepner
  • 497,756
  • 71
  • 530
  • 681