Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39212

Re: Data Tree urgent help!!!!!!

From Peter Otten <__peter__@web.de>
Subject Re: Data Tree urgent help!!!!!!
Date 2013-02-19 13:34 +0100
Organization None
References <4abbe91f-a57e-47de-9cf3-5b9574201c83@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.2018.1361277272.2939.python-list@python.org> (permalink)

Show all headers | View raw


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


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Data Tree urgent help!!!!!! anadionisio257@gmail.com - 2013-02-19 04:14 -0800
  Re: Data Tree urgent help!!!!!! Peter Otten <__peter__@web.de> - 2013-02-19 13:34 +0100
    Re: Data Tree urgent help!!!!!! Ana Dionísio <anadionisio257@gmail.com> - 2013-02-19 04:50 -0800
    Re: Data Tree urgent help!!!!!! Ana Dionísio <anadionisio257@gmail.com> - 2013-02-19 04:50 -0800
    Re: Data Tree urgent help!!!!!! Leo Breebaart <leo@lspace.org> - 2013-02-20 10:18 +0000
      Re: Data Tree urgent help!!!!!! Peter Otten <__peter__@web.de> - 2013-02-20 11:46 +0100
      Re: Data Tree urgent help!!!!!! Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-20 22:01 +1100
        Re: Data Tree urgent help!!!!!! Chris Angelico <rosuav@gmail.com> - 2013-02-20 22:16 +1100
          Re: Data Tree urgent help!!!!!! Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-20 22:47 +1100

csiph-web