XClose

An introduction to research programming with Python

Home
Menu

Solution

With this maze structure:

In [1]:
house = {
    'living' : {
        'exits': {
            'north' : 'kitchen',
            'outside' : 'garden',
            'upstairs' : 'bedroom'
        },
        'people' : ['James'],
        'capacity' : 2
    },
    'kitchen' : {
        'exits': {
            'south' : 'living'
        },
        'people' : [],
        'capacity' : 1
    },
    'garden' : {
        'exits': {
            'inside' : 'living'
        },
        'people' : ['Sue'],
        'capacity' : 3
    },
    'bedroom' : {
        'exits': {
            'downstairs' : 'living',
            'jump' : 'garden'
        },
        'people' : [],
        'capacity' : 1
    }
}

We can get a simpler dictionary with just capacities like this:

In [2]:
result = { 
    name: room['capacity']
    for name, room in house.items()
    if room['capacity'] > 0
}
print(result)
{'living': 2, 'kitchen': 1, 'garden': 3, 'bedroom': 1}
In [ ]: