n-dim permutation matrix/generator?

Discussion in 'Python' started by robert, Nov 20, 2006.

  1. robert

    robert Guest

    for the purpose of flat n-dim iteration a function or generator ndim_permute(*v) should compute or generate a list of tuples like:

    ndim_permute( (0,1,2), (0,1), (0,1), ) ->

    0 0 0
    0 0 1
    0 0 2
    0 1 0
    0 1 1
    0 1 1
    1 0 1
    .....


    what is a good solution? Or is there already a total iterator existing somewhere in the stdlib?


    robert
     
    robert, Nov 20, 2006
    #1
    1. Advertising

  2. robert wrote:

    > for the purpose of flat n-dim iteration a function or generator
    > ndim_permute(*v) should compute or generate a list of tuples like:
    >
    > ndim_permute( (0,1,2), (0,1), (0,1), ) ->
    >
    > 0 0 0
    > 0 0 1
    > 0 0 2
    > 0 1 0
    > 0 1 1
    > 0 1 1


    hmm ... shouldn't his be (0 1 2) ?

    > 1 0 1
    > ....
    >
    >
    > what is a good solution? Or is there already a total iterator existing
    > somewhere in the stdlib?


    I think there are good solutions in the python cookbook at:

    http://aspn.activestate.com/ASPN/Cookbook/Python

    But it gives a page mentioning 'system difficulties' now, so I can't
    check. Someone else will post the link to their favorite recipe.
    Meanwhile, this gives me a chance to plug some of my experimental code :)

    A.

    def ncycle(seq,n):
    while True:
    for x in seq:
    for dummy in xrange(n):
    yield x

    def cross(*args):
    p,R = 1,[]
    for arg in args:
    L = list(arg)
    R.append(ncycle(L,p))
    p *= len(L)
    R.reverse()
    for dummy in xrange(p):
    yield tuple(x.next() for x in R)

    def test():
    for x in cross((0,1,2), (0,1), (0,1)):
    print x

    if __name__=='__main__':
    test()
     
    Anton Vredegoor, Nov 20, 2006
    #2
    1. Advertising

  3. robert

    Guest

    If your products are not too big you may use the following approach

    def cross(*L):
    P=[()]
    for i in range(len(L)):
    P_=[]
    for p in P:
    for el in L:
    P_.append(p+(el,))
    P = P_
    return P

    if __name__ == '__main__':
    for x in cross((0,1,2), (0,1), (0,1)):
    print x
     
    , Nov 20, 2006
    #3
    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. Talin

    Permutation Generator

    Talin, Aug 12, 2005, in forum: Python
    Replies:
    10
    Views:
    15,066
    Gerard Flanagan
    Aug 15, 2005
  2. Denis Palas
    Replies:
    1
    Views:
    528
  3. Jack Middleton

    Faster matrix permutation algorithm?

    Jack Middleton, Aug 13, 2005, in forum: C Programming
    Replies:
    3
    Views:
    783
    russell kym horsell
    Aug 14, 2005
  4. Replies:
    14
    Views:
    1,457
    Jorgen Grahn
    Nov 4, 2008
  5. Florian Kaufmann
    Replies:
    1
    Views:
    390
Loading...

Share This Page