Python: Get items at depth? (set library?)

Posted by Mark on Stack Overflow See other posts from Stack Overflow or by Mark
Published on 2010-04-09T20:10:33Z Indexed on 2010/04/09 20:13 UTC
Read the original article Hit count: 307

Filed under:
|
|
|

I have a nested list something like this:

PLACES = (
    ('CA', 'Canada', (
        ('AB', 'Alberta'),
        ('BC', 'British Columbia' (
            ('van', 'Vancouver'),
        ),
        ...
    )),
    ('US', 'United States', (
        ('AL', 'Alabama'),
        ('AK', 'Alaska'),
        ...

I need to retrieve some data out of it. If depth is 0 I need to retrieve all the countries (and their codes), if depth == 1, I need to retrieve all the states/provinces, if depth == 2 I need to retrieve all the cities... and so forth. Is there some set library for doing stuff like this? Or can someone point me in the right direction? I started coding up a solution only to realize it wouldn't work for levels deeper than 1 because you have to go in and out of each list...

© Stack Overflow or respective owner

Related posts about python

Related posts about algorithms