[Tutor] Multiple Dimension (array/list/whatever it is are).

Michael Janssen Janssen at rz.uni-frankfurt.de
Mon May 10 05:25:06 EDT 2004


On Mon, 10 May 2004, George Patterson wrote:

> Hi All.
>
>
>
>
> A could of quick questions:
>
> 1. What is the data structure below called? Apart from G :-P Is it an
> array, a list or something else. The original article was very vague on
> the commenting. See bottom of email for URL.
>
> G = {'s':{'u':10, 'x':1},
>      'u':{'v':1, 'x':2},
>      'v':{'y':4},
>      'x':{'u':3, 'v':9, 'y':2},
>      'y':{'s':7, 'v':6}
>     }

it's a dictionary mapping keys s,u,v,x,y to subdictionaries. Read more
tutorials to get this:
http://www.python.org/doc/current/tut/tut.html  Section 5.4
http://www.python.g2swaroop.net/  Chapter 9

> 2. How would I alter the above data structure such that all instances of
> 'x' ends up being equal to 5. As per sample below.
>
> {'s':{'u':10, 'x':5},
>      'u':{'v':1, 'x':5},
>      'v':{'y':4},
>      'x':{'u':3, 'v':9, 'y':2},
>      'y':{'s':7, 'v':6}
> }


first you will need to iterate through the toplevel dictionary. Then you
should test if the dictionary has a key "x" and set it to 5.

So you have to figure out, how to iterate ("for-loop") through a
dictionary, how to test the existence of a key and how to reassign it.
Might be a good training for you, so I won't spoil it with a solution.


Michael



More information about the Tutor mailing list