- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
15 个中!数字 1-15 的可能排列,我需要选择 10!他们是随机的。
不幸的是,虽然 this answer 中的方法如果我迭代前 10 个排列,可以避免存储所有排列并对其进行洗牌时遇到的内存不足问题!使用 itertools.permutations 返回的迭代器进行排列,它们都将按顺序排列。对我来说,获得排列的随机抽样(没有重复)非常重要。
from itertools import permutations
from random import shuffle
from math import factorial
count=15
placements = list(permutations(range(count), count))
shuffle(placements)
for placement in placements[:factorial(10)]:
// do something with placement
我尝试了以下方法,但不能保证它不会选择相同的排列两次:
from math import factorial
from random import sample
count=15
for notused in range(factorial(10)):
placement = sample(range(count),count)
\\ do something with placement
目前正在尝试以下基于 this answer 的方法:
from math import factorial
from random import sample
placements = set()
count = 15
cap = factorial(10)
while len(placements) < cap:
placements.add(tuple(sample(range(count),count)))
for placement in placements:
\\ do something with placement
最佳答案
如果我没记错的话,你的情况下的排列基本上就是以一种没有数字重复的方式对列表进行洗牌,也许你可以这样做:
from random import shuffle
samps = []
count = 0
while count < 10:
j = list(range(15))
shuffle(j)
if j not in samps:
samps.append(j)
count += 1
这给出:
[[0, 11, 13, 14, 1, 9, 6, 4, 5, 3, 7, 10, 2, 12, 8],
[10, 1, 9, 2, 4, 0, 13, 14, 5, 8, 12, 7, 11, 3, 6],
[3, 13, 6, 4, 12, 5, 0, 2, 10, 7, 1, 8, 11, 9, 14],
[1, 10, 13, 7, 11, 9, 8, 4, 14, 0, 12, 2, 3, 6, 5],
[9, 8, 7, 11, 3, 10, 6, 5, 4, 0, 14, 12, 1, 13, 2],
[3, 11, 6, 8, 1, 4, 12, 14, 7, 5, 13, 0, 10, 9, 2],
[5, 13, 8, 3, 0, 9, 1, 4, 11, 12, 6, 14, 2, 10, 7],
[11, 1, 0, 2, 13, 12, 14, 3, 6, 10, 9, 7, 4, 8, 5],
[12, 10, 6, 7, 2, 13, 3, 0, 1, 8, 4, 11, 14, 5, 9],
[2, 5, 7, 9, 4, 12, 14, 6, 3, 10, 8, 13, 11, 0, 1]]
关于改组 itertools.permutation(range(15)) 时出现 Python OOM 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53982678/
Numpy documentation在 np.random.permutation建议所有新代码使用 np.random.default_rng()来自随机生成器包。我在文档中看到,Random G
是否有任何已知的算法如何有效地生成具有附加限制的任何随机多集排列。 例子: 我有多个项目,例如:{1,1,1,2,2,3,3,3} ,以及一组限制性的集合,例如 { {3} , {1,2} , {1,
您将如何生成列表 b(1,6,8,3,9,5) 的所有可能排列包括不同长度的?例子: List a = [1,2,3] generateperms(a) 1,2,3 3,1,2 3,2,1 1,3,2
我不确定如何在限制范围内解决这个问题。 将“单词”视为大写字母 A-Z 的任何序列(不仅限于“字典单词”)。对于至少有两个不同字母的单词,还有其他单词由相同的字母组成但顺序不同(例如,STATIONA
题目地址:https://leetcode.com/problems/permutations/description/ 题目描述 Given a collection of distinct n
一行中有 n 个项目。我们必须在不能选择两个连续项目的限制下找到可以选择项目的方式数。 我试图用递归关系来做,但无法达到任何。请帮我解决问题。 最佳答案 在网上搜索后,我得到了上述问题的解决方案。 假
创建列表或集合的排列非常简单。我需要将函数应用于列表中所有元素的所有子集的每个元素,按照它们出现的顺序。例如: apply f [x,y] = { [x,y], [f x, y], [x, f y],
我遇到了一个看似简单的问题,有人可以帮忙吗? 我有两个列表 a和 b .我可以将列表的元素称为 a[i][j]其中 0
我正在将一个程序从 matlab 翻译成 Python。 matlab代码使用permute方法: B = PERMUTE(A,ORDER) rearranges the dimensions of
谜题 穷举一个数组中各个元素的排列 策略 减而治之、递归 JavaScript解 复制代码 代码如下:
题目地址:https://leetcode.com/problems/next-permutation/description/ 题目描述 Implement next permutation,
从我关于"Using SIMD AVX SSE for tree traversal" ive的另一个问题中,我得到了这个试图进行基准测试的代码。之前我没有对SIMD做任何事情,所以我对这种排列方式有
这段代码为我提供了长度为 x 的 n 值的所有可能组合,总和为 n。 function GETall_distri_pres($n_valeurs, $x_entrees, $combi_presen
如果我想找到列表的排列,我知道排列的数量由多项系数给出。例如,“MISSISSIPPI”有 11 个字母,“S”出现 4 次,“I”出现 4 次,“P”出现两次,“M”出现一次。因此“MISSISSI
我有一个伪代码,我已经将其翻译成java代码,但是每当我运行该代码时,我都会得到一个空的数组列表,但它应该给我一个随机的整数列表。这是伪代码: Algorithm 1. RandPerm(N) Inp
我想计算适合弗里德曼检验的分块设计的所有排列。考虑以下示例: thedata p dim(p) [1] 1295 12 R> head(p) [,1] [,2] [,3] [,4]
这与其说是编程问题,不如说是数学问题,但我认为这里的很多人都非常擅长数学! :) 我的问题是:给定一个 9 x 9 的网格(81 个单元格),其中必须包含数字 1 到 9,每个数字恰好出现 9 次,可
我有这个变量:message = "Hello World"我构建了一个对其进行洗牌的函数: def encrypt3(message,key): random.seed(key) l
以下程序使用 itertools.permutations 从列表中构造一个 URL。 def url_construct_function(): for i in range(1, len(
我有 36 个人和 6 张 table 。我想围绕每张 table 组成 6 个小组。然后再组成 6 个其他组,再组成 6 个其他组……直到每个人都遇到每个人,但没有人遇到两次。 到目前为止,我想出了
我是一名优秀的程序员,十分优秀!