how to generate permutations of array in python?

Question:

i have an array of 27 elements,and i don’t want to generate all permutations of array (27!)
i need 5000 randomly choosed permutations,any tip will be useful…

Asked By: user257522

||

Answers:

itertools.permutations. It’s a generator, so it won’t create the whole list of permutations. You could skip randomly until you’ve got 5000.

Answered By: Cat Plus Plus

You may want the itertools.permutations() function. Gotta love that itertools module!

NOTE: New in 2.6

Answered By: ironfroggy
import random

perm_list = []

for i in range(5000):
    temp = range(27)
    random.shuffle(temp)
    perm_list.append(temp)

print(perm_list)

10888869450418352160768000000 I love big numbers! 🙂

AND

10888869450418352160768000001 is PRIME!!

EDIT:

#with duplicates check as suggested in the comment

perm_list = set()
while len(perm_list)<5000:
    temp = range(27)
    random.shuffle(temp)
    perm_list.add(tuple(temp)) # `tuple` because `list`s are not hashable. right Beni?

print perm_list

WARNING: This wont ever stop if RNG is bad!

Answered By: Pratik Deoghare

To generate one permutation use random.shuffle and store a copy of the result. Repeat this operation in a loop and each time check for duplicates (there probably won’t be any though). Once you have 5000 items in your result set, stop.

To address the point in the comment, Python’s random module is based on the Mersenne Twister and has a period of 2**19937-1, which is considerably larger than 27! so it should be suitable for your use.

Answered By: Mark Byers
# apermindex should be a number between 0 and factorial(len(alist))
def perm_given_index(alist, apermindex):
    for i in range(len(alist)-1):
        apermindex, j = divmod(apermindex, len(alist)-i)
        alist[i], alist[i+j] = alist[i+j], alist[i]
    return alist

Usage: perm_given_index(['a','b','c'], 3)

This uses the Lehmer code for the permutation as the values of j match that.

Answered By: Dan D.

You can try implementing the random_permutation itertools recipes. For convenience I use a third-party library, more_itertools, that implements this recipe for us:

import more_itertools as mit


iterable = range(27)
mit.random_permutation(iterable)
# (24, 3, 18, 21, 17, 22, 14, 15, 20, 8, 4, 7, 13, 6, 25, 5, 12, 1, 9, 19, 23, 11, 16, 0, 26, 2, 10)

A random permutation is created for every call of the function. We can make a generator that yields these results for n calls. We will implement this generator and demonstrate random results with an abridged example:

def random_permute_generator(iterable, n=10):
    """Yield a random permuation of an iterable n times."""
    for _ in range(n):
        yield mit.random_permutation(iterable)


list(random_permute_generator(range(10), n=20))
# [(2, 7, 9, 6, 5, 0, 1, 3, 4, 8),
#  (7, 3, 8, 1, 2, 6, 4, 5, 9, 0),
#  (2, 3, 1, 8, 7, 4, 9, 0, 6, 5),
#  (0, 5, 6, 8, 2, 3, 1, 9, 4, 7),
#  (0, 8, 1, 9, 4, 5, 7, 2, 3, 6),
#  (7, 2, 5, 8, 3, 4, 1, 0, 9, 6),
#  (9, 1, 4, 5, 8, 0, 6, 2, 7, 3),
#  (3, 6, 0, 2, 9, 7, 1, 4, 5, 8),
#  (8, 4, 0, 2, 7, 5, 6, 1, 9, 3),
#  (4, 9, 0, 5, 7, 1, 8, 3, 6, 2)
#  ...]

For your specific problem, substitute the iterable and number of calls n with the appropriate values, e.g. random_permute_generator(iterable, n=5000).

See also more_itertools docs for further information on this tool.


Details

For those interested, here is the actual recipe.

From the itertools recipes:

def random_permutation(iterable, r=None):
    "Random selection from itertools.permutations(iterable, r)"
    pool = tuple(iterable)
    r = len(pool) if r is None else r
    return tuple(random.sample(pool, r))
Answered By: pylang
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.