Dealing with Lists

stas poritskiy stascrash at gmail.com
Tue Sep 10 23:44:49 EDT 2013


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



More information about the Python-list mailing list