如何在python中生成数组的排列?

时间:2010-01-23 19:12:57

标签: python permutation

我有一个包含27个元素的数组,我不想生成数组的所有排列(27!) 我需要5000个随机选择的排列,任何提示都会有用......

6 个答案:

答案 0 :(得分:36)

要生成一个排列,请使用random.shuffle并存储结果的副本。在循环中重复此操作,每次检查重复(尽管可能不会有任何重复)。一旦结果集中有5000个项目,请停止。

为了解决评论中的要点,Python的random module基于Mersenne Twister,其周期为2**19937-1,比27!大得多,所以应该{{1}}适合您的使用。

答案 1 :(得分:11)

import random

perm_list = []

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

print(perm_list)

10888869450418352160768000000我喜欢大数字! :)

10888869450418352160768000001是PRIME !!

修改

#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

警告:如果RNG不好,这种情况永远不会停止!

答案 2 :(得分:6)

itertools.permutations。它是一个生成器,因此它不会创建整个排列列表。你可以随机跳过,直到你得到5000。

答案 3 :(得分:3)

# 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

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

这使用Lehmer代码进行排列,因为j的值与之匹配。

答案 4 :(得分:1)

您可能需要itertools.permutations()函数。一定要喜欢itertools模块!

注意:2.6中的新功能

答案 5 :(得分:0)

You can try implementing the <label>Last Name <input type="text" name="last_name" value="<?php echo $contact->last_name; ?>" placeholder="Enter Your Last Name"> </label> </div> </div> itertools recipes. For convenience I use a third-party library, more_itertools, that implements this recipe for us:

                <div class="grid-x grid-margin-x">
                    <div class="cell large-12">
                        <input class="button big abutton right small" name="submit" type="submit" value="Submit"/>
                    </div>
                </div>
            </form>

A random permutation is created for every call of the function. We can make a generator that yields these results for <button class="close-button" data-close aria-label="Close modal" type="button"> <span aria-hidden="true">&times;</span> </button> </div> <!--The modal edit ends here--> </li> <li> <a class="button alert tiny">Delete</a> </li> </ul> calls. We will implement this generator and demonstrate random results with an abridged example:

random_permutation

For your specific problem, substitute the iterable and number of calls 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) with the appropriate values, e.g. n.

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_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)
#  ...]