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

发布于 2024-08-18 12:56:15 字数 75 浏览 5 评论 0原文

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

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

苦妄 2024-08-25 12:56:15

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

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

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.

征棹 2024-08-25 12:56:15
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 很糟糕,这将永远不会停止!

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!

两仪 2024-08-25 12:56:15

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

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.

半透明的墙 2024-08-25 12:56:15
# 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 的值与其匹配。

# 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.

梦里兽 2024-08-25 12:56:15

您可以尝试实现 random_permutation itertools Recipe< /a>.为了方便起见,我使用第三方库 more_itertools,为我们实现了这个秘诀:

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)

为函数的每次调用创建一个随机排列。我们可以制作一个生成器,为 n 调用生成这些结果。我们将实现这个生成器并通过一个简短的示例演示随机结果:

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

对于您的具体问题,将可迭代次数和调用次数 n 替换为适当的值,例如 random_permute_generator(iterable, n=5000 )

另请参阅 more_itertools 文档 有关此工具的更多信息。


详细信息

对于那些有兴趣的人,这里是实际的食谱。

来自 itertools 食谱

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))

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))
つ低調成傷 2024-08-25 12:56:15

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

注意:2.6 中的新增功能

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

NOTE: New in 2.6

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文