[Tutor] RE: Tutor Digest, Vol 3, Issue 14

Raymond Hettinger python at rcn.com
Mon May 10 12:24:40 EDT 2004

> 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
> 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 is a directed graph, with path weights.

Think of airline routes. 

>From airport S you can fly to either airport u or airport x.   It takes
10 hours to get to u but only one hour to get to x.

> 2. How would I alter the above data structure such that all instances
> '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}
> }

for airpost, destinations in G:
    for dest, cost in destinations:
        if dest == 'x':
            destinations['x'] = 5

Raymond Hettinger

More information about the Tutor mailing list