Velocity Reviews > Data Tree urgent help!!!!!!

# Data Tree urgent help!!!!!!

Guest
Posts: n/a

 02-19-2013
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

Peter Otten
Guest
Posts: n/a

 02-19-2013
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

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

>>> class Name(str):

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

Ana Dionísio
Guest
Posts: n/a

 02-19-2013
Thank you so much!!!!!!! It works perfectly!!!

Ana Dionísio
Guest
Posts: n/a

 02-19-2013
Thank you so much!!!!!!! It works perfectly!!!

Leo Breebaart
Guest
Posts: n/a

 02-20-2013
Peter Otten <(E-Mail Removed)> writes:

> >>> class Name(str):

> ... def __repr__(self):
> ... return self
> ...
> >>> apple, pear, dog, cat, fork, spoon = map(Name, "apple pear dog cat fork spoon".split())

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

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

So I was wondering why you used Name.

--
Leo Breebaart <(E-Mail Removed)>

Peter Otten
Guest
Posts: n/a

 02-20-2013
Leo Breebaart wrote:

> Peter Otten <(E-Mail Removed)> writes:
>
>> >>> class Name(str):

>> ... def __repr__(self):
>> ... return self
>> ...
>> >>> apple, pear, dog, cat, fork, spoon = map(Name, "apple pear dog cat
>> >>> fork spoon".split())

>
> Is there any reason why you introduced the Name class? In Python
> 2.7 this works equally well if I just do:
>
>>>> apple, pear, dog, cat, fork, spoon = map(str, "apple pear dog cat fork
>>>> spoon".split())

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

>>> class Name(str):

.... def __repr__(self): return self
....
>>> class Namespace(dict):

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

Steven D'Aprano
Guest
Posts: n/a

 02-20-2013
Leo Breebaart wrote:

> Peter Otten <(E-Mail Removed)> writes:
>
>> >>> class Name(str):

>> ... def __repr__(self):
>> ... return self
>> ...
>> >>> apple, pear, dog, cat, fork, spoon = map(Name, "apple pear dog cat
>> >>> fork spoon".split())

>
> Is there any reason why you introduced the Name class? In Python
> 2.7 this works equally well if I just do:
>
>>>> apple, pear, dog, cat, fork, spoon = map(str, "apple pear dog cat fork
>>>> spoon".split())

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

--
Steven

Chris Angelico
Guest
Posts: n/a

 02-20-2013
On Wed, Feb 20, 2013 at 10:01 PM, Steven D'Aprano
<(E-Mail Removed)> wrote:
> Leo Breebaart wrote:
>
>> Peter Otten <(E-Mail Removed)> writes:
>>
>>> >>> class Name(str):
>>> ... def __repr__(self):
>>> ... return self
>>> ...
>>> >>> apple, pear, dog, cat, fork, spoon = map(Name, "apple pear dog cat
>>> >>> fork spoon".split())

>>
>> Is there any reason why you introduced the Name class? In Python
>> 2.7 this works equally well if I just do:
>>
>>>>> apple, pear, dog, cat, fork, spoon = map(str, "apple pear dog cat fork
>>>>> spoon".split())

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

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

Steven D'Aprano
Guest
Posts: n/a

 02-20-2013
Chris Angelico wrote:

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

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:

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

--
Steven