List Combinations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gerdus van Zyl

    List Combinations

    I have a list that looks like this:
    [['3'], ['9', '1'], ['5'], ['4'], ['2', '5', '8']]

    how can I get all the combinations thereof that looks like as follows:
    3,9,5,4,2
    3,1,5,4,2
    3,9,5,4,5
    3,1,5,4,5
    etc.

    Thank You,
    Gerdus
  • George Sakkis

    #2
    Re: List Combinations

    On Mar 12, 10:18 am, Gerdus van Zyl <gerdusvan...@g mail.comwrote:
    I have a list that looks like this:
    [['3'], ['9', '1'], ['5'], ['4'], ['2', '5', '8']]
    >
    how can I get all the combinations thereof that looks like as follows:
    3,9,5,4,2
    3,1,5,4,2
    3,9,5,4,5
    3,1,5,4,5
    etc.
    >
    Thank You,
    Gerdus
    Search for "cartesian product" recipes.

    George

    Comment

    • Mark Dickinson

      #3
      Re: List Combinations

      On Mar 12, 10:18 am, Gerdus van Zyl <gerdusvan...@g mail.comwrote:
      I have a list that looks like this:
      [['3'], ['9', '1'], ['5'], ['4'], ['2', '5', '8']]
      >
      how can I get all the combinations thereof that looks like as follows:
      You could wait for Python 2.6, or download the current alpha:

      Python 2.6a1+ (trunk:61353M, Mar 12 2008, 11:21:13)
      [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
      Type "help", "copyright" , "credits" or "license" for more information.
      >>from itertools import product
      >>for c in product(['3'], ['9', '1'], ['5'], ['4'], ['2', '5', '8']): print c
      ...
      ('3', '9', '5', '4', '2')
      ('3', '9', '5', '4', '5')
      ('3', '9', '5', '4', '8')
      ('3', '1', '5', '4', '2')
      ('3', '1', '5', '4', '5')
      ('3', '1', '5', '4', '8')

      If you can't wait, look at http://docs.python.org/dev/library/itertools.html
      where equivalent code is given.

      Mark

      Comment

      • Mel

        #4
        Re: List Combinations

        Gerdus van Zyl wrote:
        I have a list that looks like this:
        [['3'], ['9', '1'], ['5'], ['4'], ['2', '5', '8']]
        >
        how can I get all the combinations thereof that looks like as follows:
        3,9,5,4,2
        3,1,5,4,2
        3,9,5,4,5
        3,1,5,4,5
        etc.
        >
        Thank You,
        Gerdus
        What they said, or, if you want to see it done:


        def combi (s):
        if s:
        for a in s[0]:
        for b in combi (s[1:]):
        yield [a] + b
        else:
        yield []

        for y in combi ([['3'], ['9', '1'], ['5'], ['4'], ['2', '5', '8']]):
        print y

        Comment

        • Arnaud Delobelle

          #5
          Re: List Combinations

          On Mar 12, 3:38 pm, "Reedick, Andrew" <jr9...@ATT.COM wrote:
          [...]
          Start here
          >

          and go through the thread.  There are several ways to solve the problem
          and we evaluated the performance and 'pythonicity' of each.  
          I used a kind of extended cartesian product function a while ago while
          writing a parser. If I simplify it down to a simple product function
          it goes like this:

          def product(*sequen ces):
          i, n = 0, len(sequences)
          vals = [None]*n
          iters = map(iter, sequences)
          while i >= 0:
          if i == n:
          yield tuple(vals)
          i -= 1
          else:
          for vals[i] in iters[i]:
          i += 1
          break
          else:
          iters[i] = iter(sequences[i])
          i -= 1

          It's neither recursive nor a hack, I haven't tried to measure it
          against other approaches (obviously it wouldn't beat the eval(...)
          hack). I haven't optimised it for readability (by others) either :)

          --
          Arnaud

          Comment

          Working...