Creating Linked Lists in Python

andrew cooke andrew at acooke.org
Sat Mar 21 09:17:11 EDT 2009


andrew cooke wrote:
[...]

i messed up my example; corrected below (I hope)

> in your case you could use ints for the nodes and a dict([int]) for the
> graph.  so:
>
{1: [2,3], 2: [1,3], 3: [3]}
>
> is a graph in which 1 and 2 are connected in each direction, both 1 and 2
> are linked to 3, and 3 has a loop that links back to itself.

and to take it a little further, you might also want to store the letter
associated with the transition.  so for the NFA at
http://en.wikipedia.org/wiki/Automata_theory you'd use:

{1: [('a',1),('b',1),('a',2)], 2:[('a',3),('b',3)]}

or you could replace the list of transitions with a dict:

{1: {'a':[1,2], 'b':[1]}, 2:{'a':[3], 'b':[3]}}

andrew




More information about the Python-list mailing list