Dealing with Lists
Dave Angel
davea at davea.name
Tue Sep 10 17:51:03 EDT 2013
On 10/9/2013 17:08, stas poritskiy wrote:
> Greetings to all!
>
> i ran into a little logic problem and trying to figure it out.
>
> my case is as follows:
>
> i have a list of items each item represents a Group
>
> i need to create a set of nested groups,
>
> so, for example:
>
> myGroups = ["head", "neck", "arms", "legs"]
>
> i need to get them to be represented like this:
> (if you can imaging a folder structure)
>
> head
> |_> neck
> |_> arms
> |_>legs
I don't know what's meant by that |_> symbol. If it's really supposed
to be like subdirectories, there'd be a name associated with the
sublist. In which case you'd probably want dicts, not lists.
So I'd have to guess you want something like:
["head", ["neck", ["arms", ["legs"]]]]
To turn your list into that one, I'd write something like (untested):
def chop_up(mylist):
if len(mylist) == 1:
return mylist
return [mylist[0], chop_up(mylist[1:])]
I suspect your real problem is totally different, but this was an
interesting exercise.
--
DaveA
More information about the Python-list
mailing list