Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #53925 > unrolled thread

Dealing with Lists

Started bystas poritskiy <stascrash@gmail.com>
First post2013-09-10 14:08 -0700
Last post2013-09-11 18:17 +0200
Articles 15 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Dealing with Lists stas poritskiy <stascrash@gmail.com> - 2013-09-10 14:08 -0700
    Re: Dealing with Lists stas poritskiy <stascrash@gmail.com> - 2013-09-10 14:10 -0700
    Re: Dealing with Lists matt.komyanek@gmail.com - 2013-09-10 14:31 -0700
    Re: Dealing with Lists Dave Angel <davea@davea.name> - 2013-09-10 21:51 +0000
      Re: Dealing with Lists stas poritskiy <stascrash@gmail.com> - 2013-09-10 15:11 -0700
        Re: Dealing with Lists Roy Smith <roy@panix.com> - 2013-09-10 18:30 -0400
        Re: Dealing with Lists Dave Angel <davea@davea.name> - 2013-09-10 22:32 +0000
    Re: Dealing with Lists Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-11 02:14 +0000
      Re: Dealing with Lists Dave Angel <davea@davea.name> - 2013-09-11 02:24 +0000
        Re: Dealing with Lists Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-11 03:13 +0000
          Re: Dealing with Lists stas poritskiy <stascrash@gmail.com> - 2013-09-10 20:44 -0700
          Re: Dealing with Lists stas poritskiy <stascrash@gmail.com> - 2013-09-10 20:52 -0700
            Re: Dealing with Lists stas poritskiy <stascrash@gmail.com> - 2013-09-11 07:47 -0700
              Re: Dealing with Lists stas poritskiy <stascrash@gmail.com> - 2013-09-11 07:57 -0700
                Re: Dealing with Lists Peter Otten <__peter__@web.de> - 2013-09-11 18:17 +0200

#53925 — Dealing with Lists

Fromstas poritskiy <stascrash@gmail.com>
Date2013-09-10 14:08 -0700
SubjectDealing with Lists
Message-ID<33650fe9-802e-477d-a361-6d4bdeb28762@googlegroups.com>
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

and so on until i hit the last element.

what i thought would work (but don't know really how to advance here) is:

    def createVNTgroups(self, groupsData):
        #function recieves the LIST of group elements, INDEX0 is always the
        #MAIN-ROOT of the tree

        for i in range(len(groupsData)):
            print groupsData[i]
            
            for q in range(1, len(groupsData)):
                
                print groupsData[q]

could someone give me a hint?

thanks in advance!

[toc] | [next] | [standalone]


#53926

Fromstas poritskiy <stascrash@gmail.com>
Date2013-09-10 14:10 -0700
Message-ID<7debc7d9-159c-4fd3-ada3-4f82661ccaed@googlegroups.com>
In reply to#53925
there is a little bit more to this, but i think when i will be able to process the list the way i need it i can continue on my own. 

[toc] | [prev] | [next] | [standalone]


#53927

Frommatt.komyanek@gmail.com
Date2013-09-10 14:31 -0700
Message-ID<990d4b72-af39-4f70-9e0d-9d389d1e442e@googlegroups.com>
In reply to#53925
What you're asking is a Ragged Hierarchy.

On Tuesday, September 10, 2013 5:08:45 PM UTC-4, 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
> 
> 
> 
> and so on until i hit the last element.
> 
> 
> 
> what i thought would work (but don't know really how to advance here) is:
> 
> 
> 
>     def createVNTgroups(self, groupsData):
> 
>         #function recieves the LIST of group elements, INDEX0 is always the
> 
>         #MAIN-ROOT of the tree
> 
> 
> 
>         for i in range(len(groupsData)):
> 
>             print groupsData[i]
> 
>             
> 
>             for q in range(1, len(groupsData)):
> 
>                 
> 
>                 print groupsData[q]
> 
> 
> 
> could someone give me a hint?
> 
> 
> 
> thanks in advance!

[toc] | [prev] | [next] | [standalone]


#53928

FromDave Angel <davea@davea.name>
Date2013-09-10 21:51 +0000
Message-ID<mailman.226.1378849885.5461.python-list@python.org>
In reply to#53925
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

[toc] | [prev] | [next] | [standalone]


#53929

Fromstas poritskiy <stascrash@gmail.com>
Date2013-09-10 15:11 -0700
Message-ID<3d1eaccc-cf89-4eb0-b96c-b22a0a2f758e@googlegroups.com>
In reply to#53928
Those simbols are just for visual representation.

I need to store each of the first elements of a par, so I can reference to them as to a parent of another group.

So head is parent of neck,  while neck is parent of arms and so on.

I'm not sure I understand how to apply your chop list example, dave

[toc] | [prev] | [next] | [standalone]


#53930

FromRoy Smith <roy@panix.com>
Date2013-09-10 18:30 -0400
Message-ID<roy-7401CA.18300010092013@news.panix.com>
In reply to#53929
In article <3d1eaccc-cf89-4eb0-b96c-b22a0a2f758e@googlegroups.com>,
 stas poritskiy <stascrash@gmail.com> wrote:

> So head is parent of neck,  while neck is parent of arms and so on.

The head bone's connected to the neck bone.  The neck bone's connected 
to the arm bone...

[toc] | [prev] | [next] | [standalone]


#53931

FromDave Angel <davea@davea.name>
Date2013-09-10 22:32 +0000
Message-ID<mailman.227.1378852367.5461.python-list@python.org>
In reply to#53929
On 10/9/2013 18:11, stas poritskiy wrote:

Please include some quotation from the message you're replying to.  We
can't tell who this was responding to until the very last line, where
you mention my name.  But since you're using buggy googlegroups, you'd
better also read

 http://wiki.python.org/moin/GoogleGroupsPython

> Those simbols are just for visual representation.
>

Fine.  I just don't know what they represent.

> I need to store each of the first elements of a par, so I can reference to them as to a parent of another group.

Don't know what that means either.

>
> So head is parent of neck,  while neck is parent of arms and so on.

They're just strings, not parents of anything.  But the real question is
whether that list I described is what you wanted:


["head", ["neck", ["arms", ["legs"]]]]

>
> I'm not sure I understand how to apply your chop list example, dave

Call it with a list, and it returns a different one.  if I guessed your
intent, then the returned list will be correct.  If not, then not.

myGroups = ["head", "neck", "arms", "legs"]
my_directory = chop_up(myGroups)

print my_directory

Maybe what you were really intending was to build a class that you can
create several instances, where an attribute of each instance referred
to any children it might have, and a different attribute supplied the
instance's name.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#53937

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-09-11 02:14 +0000
Message-ID<522fd1ee$0$29988$c3e8da3$5496439d@news.astraweb.com>
In reply to#53925
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.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#53938

FromDave Angel <davea@davea.name>
Date2013-09-11 02:24 +0000
Message-ID<mailman.231.1378866311.5461.python-list@python.org>
In reply to#53937
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?  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.


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#53939

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-09-11 03:13 +0000
Message-ID<522fdfec$0$29988$c3e8da3$5496439d@news.astraweb.com>
In reply to#53938
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

[toc] | [prev] | [next] | [standalone]


#53940

Fromstas poritskiy <stascrash@gmail.com>
Date2013-09-10 20:44 -0700
Message-ID<f969fc66-fc2a-45fa-a9c1-888e489cd170@googlegroups.com>
In reply to#53939
Guys,
i appreciate the effort put in here, i guess i should've started a little wider on description.

sorry for confusion, here it goes:

I am developing a script/application that uses adobe Scene7 API. the idea here is mass process a ton of files (hundreds of thousands), we generate image files from 3d applications (3d max in our case), and each rendered file carries a specific naming convention such as this:
name_group-subgroup-subgroup_layer_type_zdepth.extention.

this translates in a nice array, that i first split using uderscore " _ ".
this results in a list that has always the same size.

Please note that in the section with group-subgroup-subroug - number of subgroups should can be ANY, and the separator there is dash " - ",

so when the first split " _ " occurs, i generate a separate element in my FIRST LIST that has all these groups. 

I then take the groups and run another split using the dash " - ", to create a new list for only groups.

In the second list, what i have is the first element at index 0 - being alwasy the most top (root) group. and all the subgroups are nested inside there. Each subgroup is a parent of the following subroup, thus - above in my diagram i used the body parts as example of logical dependency (sorry if this was unclear, again),

now, the next step is using API and its classes i create an instance of an object that generates the group for the file that will created via series of additional methods,

the file structure (think of Photoshop as an reference)
would have the main group, and a subgroup in it, and so on, depending on the type of the element that we are creating identified in the file name. (this would be using the FIRST LIST created originally)

For me to create a subgroup using the API, the first (parent) group should be already created, otherwise, i cannot use a non-existing element to assign a subgroup, 
therefore , i first my create the ROOT group located at index0
i then must make a subgroup, so the line of code would look similar to this:

#creating first group(root)
group = intance.add_group(str(name))

#creating subgroup(child of root)
subgroup = group.add_group(str(name))

since i don't know how many subgroups each file would have, i want to create a loop, that will do that for me
but i am running in a problem, trying to understand the logic for that loop.

let me know if you guys need more info from me.

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

[toc] | [prev] | [next] | [standalone]


#53941

Fromstas poritskiy <stascrash@gmail.com>
Date2013-09-10 20:52 -0700
Message-ID<1ef6a37a-e888-4ecc-a82a-477e19490723@googlegroups.com>
In reply to#53939
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

[toc] | [prev] | [next] | [standalone]


#53970

Fromstas poritskiy <stascrash@gmail.com>
Date2013-09-11 07:47 -0700
Message-ID<40438965-ec5f-409f-87af-4fb2290ec891@googlegroups.com>
In reply to#53941
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

[toc] | [prev] | [next] | [standalone]


#53971

Fromstas poritskiy <stascrash@gmail.com>
Date2013-09-11 07:57 -0700
Message-ID<5f2d171b-acbb-4ba6-96d5-3a32a5fdda59@googlegroups.com>
In reply to#53970
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

[toc] | [prev] | [next] | [standalone]


#53976

FromPeter Otten <__peter__@web.de>
Date2013-09-11 18:17 +0200
Message-ID<mailman.256.1378916294.5461.python-list@python.org>
In reply to#53971
stas poritskiy wrote:

By now your post consists of over 1300 lines, most of them containing 
nothing but quoting marks. Please be courteous to your readers and avoid 
that.

https://wiki.python.org/moin/GoogleGroupsPython

may be helpful.

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

Again, be courteous and give actual self-contained runnable Python code. I 
doubt that anyone can make much of the above
 
> any hints?

My impression is that much of your confusion stems from using generic lists 
where items in different positions have a different meaning for different 
posters and it is difficult to keep track of what's what. I suggest you bite 
the bullet and go for a class-based approach. Then, when you talk about 
"children" or "parent" it is clear that you find them in the Node's 
attribute of the same name.

Proceed to express your tasks in plain English as clearly as you can, and 
only then try to transform them into code.

To get you started here's what a simple tree might look like:

$ cat tree.py 
from __future__ import print_function

from collections import OrderedDict as Children

class Node:
    def __init__(self, name, parent=None):
        self.name = name
        self.parent = parent
        self.children = Children()
        if parent is not None:
            self.parent.children[name] = self

    def show(self, level=0):
        print("    " * level, self.name, sep="")
        for child in self.children.values():
            child.show(level + 1)

class Tree(Node):
    def __init__(self):
        Node.__init__(self, None)

    def ensure(self, path):
        """Add node with all intermediate nodes"""
        node = self
        for name in path:
            try:
                node = node.children[name]
            except KeyError:
                node =  Node(name, node)
        return node

    def show(self, level=0):
        for child in self.children.values():
            child.show()

if __name__ == "__main__":
    data = """\
plant flower tulip
plant flower lily
animal mammal cat
animal mammal zebra
animal reptile snake
animal reptile snake python
animal reptile snake boa
"""

    paths = [line.split() for line in data.splitlines()]

    tree = Tree()
    for path in paths:
        tree.ensure(path)
    tree.show()


And here's the output of the demo:

$ python tree.py
plant
    flower
        tulip
        lily
animal
    mammal
        cat
        zebra
    reptile
        snake
            python
            boa

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web