Dealing with Lists
stas poritskiy
stascrash at gmail.com
Wed Sep 11 10:57:21 EDT 2013
ok, while writing this, i figured out where those elements are located,
so each element is always at the position 0, and to get deeper i have to follow something similar to this
chopGrp = [[u'arms', [u'a', [u'super', [u'group', [u'type', [u'is', [u'here']]]]]]]
print chopGrp[1][1][1][1][0] (this would get me "type" from the list)
>>>type
this is getting somewhere, now, i am confused what type of iteration loop i should make, where each element is being called by itself,
so i could reference the previous element as its Parent.
so if i make this manually as a pseudo code:
chopGrp[0] #gets me "arms"
print "Parent is: " + chopGrp[0]
chopGrp[1][0] #gets me "a"
print "Child is: " + chopGrp[1][0]
and the next iteration should take the
chopGrp[1][0] and make it a parent of chopGrp[1][1][0]
any hints?
On Wednesday, September 11, 2013 9:47:30 AM UTC-5, stas poritskiy wrote:
> 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