Data Tree urgent help!!!!!!

A

anadionisio257

Hello!
I have this lists with information and I need to make a "tree" by associating the information inside the lists. For example:

l1 = [apple, pear]
l2 = [dog, cat]
l3 = [fork, spoon]

And I need to make something like this:

l4 = [apple, dog, fork]
l5 = [apple, dog, spoon]
l6= [apple, cat, fork]
l7 = [apple, cat, spoon]
l8 = [pear, dog, fork]
etc...

How can I do this? I could use "for" cycles and "if...else" but with larger lists it gets complicated

Is there some simple solution that I can use?

I hope you could help me
 
P

Peter Otten

Hello!
I have this lists with information and I need to make a "tree" by
associating the information inside the lists. For example:

l1 = [apple, pear]
l2 = [dog, cat]
l3 = [fork, spoon]

And I need to make something like this:

l4 = [apple, dog, fork]
l5 = [apple, dog, spoon]
l6= [apple, cat, fork]
l7 = [apple, cat, spoon]
l8 = [pear, dog, fork]
etc...

How can I do this? I could use "for" cycles and "if...else" but with
larger lists it gets complicated

Is there some simple solution that I can use?

Try itertools.product():
.... def __repr__(self):
.... return self
....
apple, pear, dog, cat, fork, spoon = map(Name, "apple pear dog cat fork spoon".split())
fruit = [apple, pear]
pets = [dog, cat]
cutlery = [fork, spoon]
from itertools import product
for item in product(fruit, pets, cutlery):
.... print item
....
(apple, dog, fork)
(apple, dog, spoon)
(apple, cat, fork)
(apple, cat, spoon)
(pear, dog, fork)
(pear, dog, spoon)
(pear, cat, fork)
(pear, cat, spoon)
 
L

Leo Breebaart

Peter Otten said:
... def __repr__(self):
... return self
...

Is there any reason why you introduced the Name class? In Python
2.7 this works equally well if I just do:

So I was wondering why you used Name.
 
P

Peter Otten

Leo said:
Is there any reason why you introduced the Name class? In Python
2.7 this works equally well if I just do:


So I was wondering why you used Name.

It was more for fun than profit ;) The OP gave

[apple, dog, fork]

in his examples, and the "normal" no-nonsense approach using a list of
strings would produce

['apple', 'dog', 'fork']

I was tempted to carry this even further with
.... def __repr__(self): return self
........ def __missing__(self, key):
.... self[key] = result = Name(key)
.... return result
....
fruit, pets, cutlery = eval("[apple, pear], [dog, cat], [fork, spoon]", Namespace())
fruit
[apple, pear]

but resisted until now...
 
S

Steven D'Aprano

Leo said:
Is there any reason why you introduced the Name class? In Python
2.7 this works equally well if I just do:


So I was wondering why you used Name.

I'm wondering why you used map.


apple, pear, dog, cat, fork, spoon = "apple pear dog cat fork spoon".split()


:)
 
C

Chris Angelico

I'm wondering why you used map.


apple, pear, dog, cat, fork, spoon = "apple pear dog cat fork spoon".split()

Why, in case someone monkeypatched split() to return something other
than strings, of course!

Sorry, I've been learning Ruby this week, and I fear it may be
damaging to my mind...

ChrisA
 
S

Steven D'Aprano

Chris said:
Why, in case someone monkeypatched split() to return something other
than strings, of course!

Sorry, I've been learning Ruby this week, and I fear it may be
damaging to my mind...

:)


You cannot monkey-patch builtins themselves in Python. Since the string is a
literal, it is guaranteed to be a builtin string, and the split method is
guaranteed to be the builtin str.split method.

This is, however, not true in the earlier example of map(str, ...) since
either of map or str could be shadowed by a global function of the same
name, or even monkey-patched across the whole Python environment:

py> import __builtin__ # Python 2.7
py> __builtin__.str = lambda x: len(x)
py> __builtin__.map = lambda func, items: [10000+func(x) for x in items]
py> map(str, "apple, pear, dog".split())
[10006, 10005, 10003]


So unlike Ruby, Python restricts what you can monkey-patch, and discourages
you from doing it even when you can. Oh, and I can reverse my monkey-patch
easily:

py> reload(__builtin__)
<module '__builtin__' (built-in)>
py> map(str, "apple, pear, dog".split())
['apple,', 'pear,', 'dog']
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top