Data Tree urgent help!!!!!!

Peter Otten __peter__ at web.de
Tue Feb 19 13:34:40 CET 2013


anadionisio257 at gmail.com 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)






More information about the Python-list mailing list