Combining arbitrary lists

Discussion in 'Python' started by Nick, Nov 15, 2004.

  1. Nick

    Nick Guest

    Given that

    n = [ [1, 2, 3], [4, 5, 6], [7, 8] ]

    then the following code produces what I expect

    for x in n[0]:
    for y in n[1]:
    for z in n[2]:
    print [x, y, z]

    --output--
    [1, 4, 7]
    [1, 4, 8]
    [1, 5, 7]
    [1, 5, 8]
    ...
    [3, 6, 8]
    -- --

    How can I do this for an arbirary length of n? This reminds me of those
    horrible fraction questions in 1st year comp. sci.
    x = 1 + 1/(1 + 1/(1 + 1/(..... )))))... which leads me to suspect that
    the simplest solution is recursive... hmmm... I'll take any suggestions.



    Muchos Gracias
    Nick.
     
    Nick, Nov 15, 2004
    #1
    1. Advertising

  2. Nick wrote:
    > Given that
    >
    > n = [ [1, 2, 3], [4, 5, 6], [7, 8] ]
    >
    > then the following code produces what I expect
    >
    > for x in n[0]:
    > for y in n[1]:
    > for z in n[2]:
    > print [x, y, z]


    ....
    >
    > How can I do this for an arbirary length of n?


    I think this is what you're looking for:
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302478

    > Muchos Gracias


    De nada ;)

    --
    Mariano
     
    Mariano Draghi, Nov 15, 2004
    #2
    1. Advertising

  3. Nick

    Nick Guest

    Mariano Draghi wrote:
    > Nick wrote:
    >
    >> Given that
    >>
    >> n = [ [1, 2, 3], [4, 5, 6], [7, 8] ]
    >>
    >> then the following code produces what I expect
    >>
    >> for x in n[0]:
    >> for y in n[1]:
    >> for z in n[2]:
    >> print [x, y, z]

    >
    >
    > ...
    >
    >>
    >> How can I do this for an arbirary length of n?

    >
    >
    > I think this is what you're looking for:
    > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302478
    >
    >> Muchos Gracias

    >
    >
    > De nada ;)
    >

    Okay... a) THANK-YOU, and b) HOW did you find that?

    Nick.
     
    Nick, Nov 15, 2004
    #3
  4. Mariano Draghi, Nov 15, 2004
    #4
  5. On Mon, 15 Nov 2004 00:57:03 -0300, Mariano Draghi <> wrote:

    >Nick wrote:
    >> Given that
    >>
    >> n = [ [1, 2, 3], [4, 5, 6], [7, 8] ]
    >>
    >> then the following code produces what I expect
    >>
    >> for x in n[0]:
    >> for y in n[1]:
    >> for z in n[2]:
    >> print [x, y, z]

    >
    >...
    >>
    >> How can I do this for an arbirary length of n?

    >
    >I think this is what you're looking for:
    >http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302478
    >

    Following is a slightly more compact generator (unless I goofed ;-)
    (untested beyoud what you see here; probably traded a little speed for code lines)

    >>> n

    [[1, 2, 3], [4, 5, 6], [7, 8]]

    >>> def doit(LOL):

    ... if not LOL: yield []; return
    ... for h in LOL[0]:
    ... for t in doit(LOL[1:]):
    ... yield [h] + t
    ...
    >>> for row in doit(n): print row

    ...
    [1, 4, 7]
    [1, 4, 8]
    [1, 5, 7]
    [1, 5, 8]
    [1, 6, 7]
    [1, 6, 8]
    [2, 4, 7]
    [2, 4, 8]
    [2, 5, 7]
    [2, 5, 8]
    [2, 6, 7]
    [2, 6, 8]
    [3, 4, 7]
    [3, 4, 8]
    [3, 5, 7]
    [3, 5, 8]
    [3, 6, 7]
    [3, 6, 8]

    >>> for row in doit([[1,2],[3,4,5]]): print row

    ...
    [1, 3]
    [1, 4]
    [1, 5]
    [2, 3]
    [2, 4]
    [2, 5]


    Regards,
    Bengt Richter
     
    Bengt Richter, Nov 15, 2004
    #5
  6. * Nick (2004-11-15 04:28 +0100)
    > Given that
    >
    > n = [ [1, 2, 3], [4, 5, 6], [7, 8] ]
    >
    > then the following code produces what I expect
    >
    > for x in n[0]:
    > for y in n[1]:
    > for z in n[2]:
    > print [x, y, z]
    >
    > --output--
    > [1, 4, 7]
    > [1, 4, 8]
    > [1, 5, 7]
    > [1, 5, 8]
    > ...
    > [3, 6, 8]
    > -- --
    >
    > How can I do this for an arbirary length of n?


    This is the Cartesian product of three sets. Have a look at
    http://www.thorstenkampe.de/python/utils.py:

    def cartes(seq0, seq1, modus = 'pair'):
    """ return the Cartesian Product of two sequences """
    if modus == 'pair':
    return [[item0, item1] for item0 in seq0 for item1 in seq1]
    elif modus == 'triple':
    return [item0 + [item1] for item0 in seq0 for item1 in seq1]

    Then you would generate your output like this:
    >>> cartes(cartes([1, 2, 3], [4, 5, 6]), [7, 8], 'triple')
     
    Thorsten Kampe, Nov 15, 2004
    #6
  7. Nick

    Steve Holden Guest

    Thorsten Kampe wrote:

    > * Nick (2004-11-15 04:28 +0100)
    >
    >>Given that
    >>
    >>n = [ [1, 2, 3], [4, 5, 6], [7, 8] ]
    >>
    >>then the following code produces what I expect
    >>
    >>for x in n[0]:
    >> for y in n[1]:
    >> for z in n[2]:
    >> print [x, y, z]
    >>
    >>--output--
    >>[1, 4, 7]
    >>[1, 4, 8]
    >>[1, 5, 7]
    >>[1, 5, 8]
    >> ...
    >>[3, 6, 8]
    >>-- --
    >>
    >>How can I do this for an arbirary length of n?

    >
    >
    > This is the Cartesian product of three sets. Have a look at
    > http://www.thorstenkampe.de/python/utils.py:
    >
    > def cartes(seq0, seq1, modus = 'pair'):
    > """ return the Cartesian Product of two sequences """
    > if modus == 'pair':
    > return [[item0, item1] for item0 in seq0 for item1 in seq1]
    > elif modus == 'triple':
    > return [item0 + [item1] for item0 in seq0 for item1 in seq1]
    >
    > Then you would generate your output like this:
    >
    >>>>cartes(cartes([1, 2, 3], [4, 5, 6]), [7, 8], 'triple')


    I think the point of the question was to act on an arbitrary number of
    length-3 lists.

    regards
    Steve
    --
    http://www.holdenweb.com
    http://pydish.holdenweb.com
    Holden Web LLC +1 800 494 3119
     
    Steve Holden, Nov 15, 2004
    #7
  8. Nick

    N Chackowsky Guest

    Thanks to all who replied; very helpful. Bengt Richter: that's a
    lovely generator--I'm new to the whole idea of generators, but that's
    very clear. I will experiment and test it.

    Nick.
     
    N Chackowsky, Nov 15, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JustSomeGuy

    Sorting lists of lists...

    JustSomeGuy, Jun 17, 2004, in forum: C++
    Replies:
    0
    Views:
    324
    JustSomeGuy
    Jun 17, 2004
  2. Honestmath
    Replies:
    5
    Views:
    559
    Honestmath
    Dec 13, 2004
  3. Jon Slaughter

    lists of lists

    Jon Slaughter, Dec 13, 2004, in forum: C++
    Replies:
    4
    Views:
    425
    Buster
    Dec 13, 2004
  4. Frans Englich

    Refactoring; arbitrary expression in lists

    Frans Englich, Jan 12, 2005, in forum: Python
    Replies:
    15
    Views:
    531
    Steve Holden
    Jan 14, 2005
  5. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    408
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
Loading...

Share This Page