[Tutor] Tree Ctrl Data Structure - Help, please!

John Fouhy john at fouhy.net
Wed Jul 16 02:09:32 CEST 2008


On 16/07/2008, Michiel Overtoom <motoom at xs4all.nl> 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.


More information about the Tutor mailing list