Data Tree

This is the place for queries that don't fit in any of the other categories.

Data Tree

Postby hum » Tue Feb 19, 2013 11:35 am

Hello! I'm new here and an absolute beginner in python, I hope that you can help me.

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 ;)
hum
 
Posts: 3
Joined: Tue Feb 19, 2013 11:25 am

Re: Data Tree

Postby Yoriz » Tue Feb 19, 2013 1:29 pm

I read over this a few times and cant make sense of the relation between lists l1 to l3 and then lists 14 to l8 :? , do you have have some code you wrote so far.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 781
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Data Tree

Postby Mekire » Tue Feb 19, 2013 1:53 pm

hum wrote:Hello! I'm new here and an absolute beginner in python, I hope that you can help me.

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


If you are trying to produce every possible combination that includes only one item from each of the first three lists then you could do this pretty easily by nesting for loops... you would need the same number of loops as the number of initial lists.

But yeah... lets see an attempt and work from there.

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Data Tree

Postby hum » Tue Feb 19, 2013 2:57 pm

Hi!

I've made this:

Code: Select all
l1 = ["apple", "pear"]
l2 = ["dog", "cat"]
l3 = ["fork", "spoon"]

i1 = iter(l1)
i2 = iter(l2)
i3 = iter(l3)

for i1 in l1:
    if i1 == "apple":
        a1 = l1[0] + ";" + l2[0]
        a2 = l1[0] + ";" + l2[1]
    else:
        a3 = l1[1] + ";" + l2[0]
        a4 = l1[1] + ";" + l2[1]

aux1 = a1
aux2 = a2
aux3 = a3
aux4 = a4

for i2 in l2:
    if i2 == "dog":
        a5 = aux1 + ";" + l3[0]
        print a5
        a6 = aux1 + ";" + l3[1]
        print a6
        a7 = aux3 + ";" + l3[0]
        print a7
        a8 = aux3 + ";" + l3[1]
        print a8
    else:
        a9 = aux2 + ";" + l3[0]
        print a9
        a10 = aux2 + ";" + l3[1]
        print a10
        a11 = aux4 + ";" + l3[0]
        print a11
        a12 = aux4 + ";" + l3[1]
        print a12


And it prints:

apple;dog;fork
apple;dog;spoon
pear;dog;fork
pear;dog;spoon
apple;cat;fork
apple;cat;spoon
pear;cat;fork
pear;cat;spoon

The code does what i want but it is extensive and confusing and, with more lists with more variables I think it is unpractical.
hum
 
Posts: 3
Joined: Tue Feb 19, 2013 11:25 am

Re: Data Tree

Postby Mekire » Tue Feb 19, 2013 3:07 pm

Wow....

You are making things wayyyyyyyyyyyyyyyyyyyy too complicated.
Just iterate over the three lists collecting results:
Code: Select all
mylists = []
for i in l1:
    for j in l2:
        for k in l3:
            mylists.append([i,j,k])

for ele in mylists:
    print(ele)


The data can also be generated in a single line list comprehension but lets stick with this for now.

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Data Tree

Postby ichabod801 » Tue Feb 19, 2013 3:17 pm

You can also do this with the itertools module mentioned in another thread:

Code: Select all
import itertools

a = ['apple', 'pear']
b = ['dog', 'cat']
c = ['fork', 'spoon']

x = list(itertools.product(a, b, c))


However, knowing Mekire's method is good in case you need to do something else while building the list of all combinations.
Craig "Ichabod" O'Brien
Minimalist, buddhist, theist, and programmer
Current languages: Python, SAS, and C++
Previous serious languages: R, Java, VBA, Lisp, HyperTalk, BASIC
ichabod801
 
Posts: 84
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Re: Data Tree

Postby zeycus » Tue Feb 19, 2013 3:18 pm

Or you could just use the product function in the itertools module. It does exactly
what (I think) you want. It is more general, meaning that you do not need to change
the code with yet-another-nested loop for each new list:

Code: Select all
from itertools import product

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

for tup in product(l1, l2, l3):
    print(tup)

I hope it helped.

EDIT: Exactly what ichabod801 suggested, but one minute later :)
Last edited by zeycus on Tue Feb 19, 2013 10:21 pm, edited 1 time in total.
Image

Live long and prosper.
Spock
User avatar
zeycus
 
Posts: 23
Joined: Sun Feb 17, 2013 10:30 am
Location: Madrid

Re: Data Tree

Postby Mekire » Tue Feb 19, 2013 3:22 pm

ichabod801 wrote:You can also do this with the itertools module mentioned in another thread


Haha... I was getting to itertools. Was gonna try to let this sink in first. Sounds like he needs some basics to begin with. :p

Cheers,
-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Data Tree

Postby hum » Tue Feb 19, 2013 4:01 pm

Mekire wrote:Wow....

You are making things wayyyyyyyyyyyyyyyyyyyy too complicated.
Just iterate over the three lists collecting results:
Code: Select all
mylists = []
for i in l1:
    for j in l2:
        for k in l3:
            mylists.append([i,j,k])

for ele in mylists:
    print(ele)


The data can also be generated in a single line list comprehension but lets stick with this for now.

-Mek


Wow, it's so simple! Thank you! ;)

And thanks to ichabod801 and zeycus too for your sugestions!
hum
 
Posts: 3
Joined: Tue Feb 19, 2013 11:25 am


Return to General Coding Help

Who is online

Users browsing this forum: snippsat and 4 guests