Dealing with Lists

S

stas poritskiy

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

for q in range(1, len(groupsData)):

print groupsData[q]

could someone give me a hint?

thanks in advance!
 
S

stas poritskiy

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.
 
D

Dave Angel

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.
 
S

stas poritskiy

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
 
R

Roy Smith

stas poritskiy said:
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...
 
D

Dave Angel

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.
 
S

Steven D'Aprano

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.
 
D

Dave Angel

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:
 
S

Steven D'Aprano

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:

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.


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
 
S

stas poritskiy

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



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.







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
 
S

stas poritskiy

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



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.







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
 
S

stas poritskiy

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!


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 10/9/2013 22:14, Steven D'Aprano 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
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:
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)

|_> neck
|_> arms

and so on until i hit the last element.
[end quote]
so I just provided that.
i need to create a set of nested groups,
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

{'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 == {}:

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

print "%s:-" % key
print_nested_dict(subdict, level+1)

d = {'head': {'neck': {'legs': {'toes': {}}, 'arms': {'thumbs': {},
'fingers': {}}}}}
we can see this output:
py> print_nested_dict(d)

|_> neck:-
|_> arms:-
|_> fingers
|_> thumbs
|_> legs:-
|_> toes

Steven
 
S

stas poritskiy

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)
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?

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!





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 10/9/2013 22:14, Steven D'Aprano 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
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:
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)
and so on until i hit the last element.
[end quote]
so I just provided that.
i need to create a set of nested groups,
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
{'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 == {}:
for key, subdict in sorted(adict.items()):
if level != 0:
spaces = ' '*4
indent = spaces*(level-1) + ' '
print (indent + '|_>'), # note comma
if subdict == {}:
print key
print "%s:-" % key
print_nested_dict(subdict, level+1)
d = {'head': {'neck': {'legs': {'toes': {}}, 'arms': {'thumbs': {},
'fingers': {}}}}}
we can see this output:
py> print_nested_dict(d)
|_> neck:-
|_> arms:-
|_> fingers
|_> thumbs
|_> legs:-
 
P

Peter Otten

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top