On 16/07/2008, Michiel Overtoom <[EMAIL PROTECTED]> wrote:
> Lauren wrote...
>  > Based on some research I've done,  I think I want my data structure to
>  > be a series of lists within lists:
>  # start of example
>
>  # this example uses a hierarchy of three levels: 1) continent, 2)
>  country/state, and 3) city
>  geo=[
>     "australia",
>     "europe",[
>         "spain",
>         "germany",
>         "belgium",
>         ],
>     "america",[
>         "california",[
>             "los angeles",
>             "san francisco",
>             "berkeley"
>             ],
>         "texas",
>         "utah"
>         ],
>     "asia"
>     ]

I think the normal way of representing a tree in python would be as
(value, children) tuples.

In this case, you would have:

geo = [('australia', []), ('europe', [('spain', []), ('germany', []),
('belgium', [])]),
           ('america', [('california', [('los angeles', []), ('san
francisco', [])]),
                             ('texas', [('detroit', [])])]),
           # etc
          ]

This lets you avoid messy isinstance testing to figure out if you've
got a value or a list of children.
(admittedly, it can be hard to keep track of the brackets, but good
indentation and a monospaced font should help out)

-- 
John.
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to