Dealing with Lists

stas poritskiy stascrash at gmail.com
Wed Sep 11 10:47:30 EDT 2013


ok, so i think that getting the nested list is a little more for what i need,
however, i could be wrong here,
i got confused with Dave's suggestion. so, i got my lists broken up in a list of lists, but how would i access each of the elements?
When i implement this solution my output list (the new one, after CHOPPING) prints like this:

[u'arms', [u'a', [u'super', [u'group', [u'type', [u'is', [u'here']]]]]]]
so i assume the "indexing" here would be similar to this:
    0                                1
             0                       1
                     0           1
                                0     1
                                          0              1
                                                             0           
[u'arms', [u'a', [u'super', [u'group', [u'type', [u'is', [u'here']]]]]]]

Basically speaking ( each generated list would be the len=2 ?

so, in my understanding, if i need to call the parenet for my FIRST group(most fist created),
i would go and call
chopGrps[0] 
this would get me "arms"
and if i need to get the second element, (which should be "a"
i would call chopGrps[1]
but instead, i get the REST of the lists stored there.

so what position does "a" take in this nested list?
and this same question would apply to the rest of the items

Thanks guys!


On Tuesday, September 10, 2013 10:52:20 PM UTC-5, stas poritskiy wrote:
> Steven,
> 
> i think you got on the right track with your proposal,
> 
> although i am not going after the "visual" represenatation that you were able to create, rather a structural one, i think this might work for me,
> 
> instead of printing, i could be using my commands to make elements (instances of API objects to create my groups inside the file)
> 
> 
> 
> but one question i might have is :
> 
> 
> 
> once i created first instance object which in my example is :
> 
> groups = intance.add_group(str(name))
> 
> 
> 
> how would i temporary preserve this name as a reference for the next iteration for the subroups?
> 
> 
> 
> On Tuesday, September 10, 2013 10:13:48 PM UTC-5, Steven D'Aprano wrote:
> 
> > 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