Dealing with Lists

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Sep 10 23:13:48 EDT 2013


On Wed, 11 Sep 2013 02:24:44 +0000, Dave Angel wrote:

> On 10/9/2013 22:14, Steven D'Aprano wrote:
> 
>> On Tue, 10 Sep 2013 14:08:45 -0700, 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"]
>>
>>
>> What is the rule for grouping these items? Below, you suggest:
>>
>> head encloses neck
>> neck encloses arms
>> arms encloses legs
>>
>> which seems rather strange. But if it is *always* the case that each
>> item encloses the next item:
>>
>> def print_nested_list(alist):
>>     spaces = ' '*4
>>     for level, item in enumerate(alist):
>>         if level != 0:
>>             indent = spaces*(level-1) + '  '
>>             print (indent + '|_>'),  # note comma
>>         print item
>>
>>
>> which gives us this:
>>
>> py> print_nested_list(['head', 'neck', 'arms', 'legs']) head
>>   |_> neck
>>       |_> arms
>>           |_> legs
>>
>>
>> as requested.
>>
>>
>>
> Very nice.  But what I want to know is how did you know that Stan (the
> OP) wanted his printed output to be formatted that way?

I don't. Stan's email is unclear. But he does show an example:

[quote]
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

and so on until i hit the last element.
[end quote]


so I just provided that.

> He said:
> 
>>>>>> i need to create a set of nested groups,
> and
>>>>>> store each of the first elements of a par, so I can reference to
>>>>>> them as to a parent of another group.


I have no idea what that means :-)


I *guess* that what Stan is actually looking for is something like a 
dictionary-based solution, not lists:

{'head': {'neck': {'arms': {}, 'legs': {}}}}

which gives:

head encloses neck
neck encloses arms and legs
arms enclose nothing
legs enclose nothing


or:

{'head': {'neck': {'arms': {'legs': {}}}}}

which gives:

head encloses neck
neck encloses arms
arms encloses legs
legs enclose nothing


but I can't really tell for sure. In this second case, using dicts, I 
might try something like this recursive solution:


def print_nested_dict(adict, level=0):
    if adict == {}:
        return
    for key, subdict in sorted(adict.items()):
        if level != 0:
            spaces = ' '*4
            indent = spaces*(level-1) + '  '
            print (indent + '|_>'),  # note comma
        if subdict == {}:
            print key
        else:
            print "%s:-" % key
            print_nested_dict(subdict, level+1)


Given:

d = {'head': {'neck': {'legs': {'toes': {}}, 'arms': {'thumbs': {}, 
'fingers': {}}}}}

we can see this output:

py> print_nested_dict(d)
head:-
  |_> neck:-
      |_> arms:-
          |_> fingers
          |_> thumbs
      |_> legs:-
          |_> toes




-- 
Steven



More information about the Python-list mailing list