473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

n-dim permutation matrix/generator?

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
Nov 20 '06 #1
2 3024
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()



Nov 20 '06 #2
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[i]:
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 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Talin | last post by:
I'm sure I am not the first person to do this, but I wanted to share this: a generator which returns all permutations of a list: def permute( lst ): if len( lst ) == 1: yield lst else: head =...
3
by: Jack Middleton | last post by:
Hi! I'm lookin for a faster permutation algorithm for matrices. I know that it can be done with multiplying a matrix with a permutation matrix. It just seems a waste to iterate through all those...
1
by: user | last post by:
Hello I have Array of 50 ints. I want to receive random permutation, so in each int will be different number from 0-49. Is there any class for permutation ? Thanx Michal
2
by: luke.yolanda | last post by:
Hi everyone Now i'm designing a random instances generator for maximum clique problem using C. So I planing to implement a adjacent matrix in this generator to store the whole graph going to be...
4
by: rrs.matrix | last post by:
this is my version of permutation and combinations. is this piece of code correct. #include<stdio.h> int a={1,2,3,4}; permute(int * b,int n) { int i;
1
by: Schüle Daniel | last post by:
Hello, I came up with this algorithm to generate all permutations it's not the best one, but it's easy enough # lst = list with objects def permute3(lst): tmp = lenlst = len(lst) def...
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
1
by: cnb | last post by:
, 'argmax', 'argmin', 'argsort', 'astype', 'base', 'byteswap', 'choose', 'clip', 'compress', 'conj', 'conjugate', 'copy', 'ctypes', 'cumprod', 'cumsum', 'data', 'diagonal', 'dtype', 'dump',...
13
by: sillyhat | last post by:
Hello, can someone please help. I found the following code at http://code.activestate.com/recipes/252178/ def all_perms(str): if len(str) <=1: yield str else: for perm in all_perms(str):...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.