Best way for permutating using multiple lists?

H

hob

if __name__ == '__main__':
permutations = list()
for i in list('def'):
for j in list('abc'):
for k in list('mno'):
for l in list('mno'):
for m in list('wxyz'):
permutations.append()
print '\t'.join("%s" % i for i in permutations)

I'd rather find a way that can perform a permutation over any number
of lists. I know there should be a way of doing this but my head isn't
working today, any advice people?
 
J

Jon Clements

if __name__ == '__main__':
    permutations = list()
    for i in list('def'):
        for j in list('abc'):
            for k in list('mno'):
                for l in list('mno'):
                    for m in list('wxyz'):
                        permutations.append()
    print '\t'.join("%s" % i for i in permutations)

I'd rather find a way that can perform a permutation over any number
of lists. I know there should be a way of doing this but my head isn't
working today, any advice people?

itertools.permutations (maybe with itertools.chain)?

hth
Jon.
 
D

Dave Angel

Michiel said:
import itertools
alt=itertools.product("def","abc","mno","mno","wxyz")
Note to the OP. You called this permutation, but it's not. It's the
crossproduct. Thus itertools.product

DaveA
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,295
Latest member
EmilG1510

Latest Threads

Top