- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
拉丁方是一个充满 n 个不同符号的 nxn 数组,每个符号在每行中恰好出现一次,在每列中恰好出现一次(如数独)。拉丁方的一个例子:
1 2 3
2 3 1
3 1 2
这是我尝试过的,但仍然不完全不同
grid = []
temp = []
block = [[1,2,3],
[2,3,1],
[3,1,2]]
perm = permutations(block)
for i in perm: #row permutations
temp.extend(i)
if len(temp)==3:
grid.extend([temp])
temp = []
for perm in zip(permutations(block[0]), permutations(block[1]), permutations(block[2])): #column permutations
temp.extend([perm])
for i in range(len(temp)): #convert to list
temp[i] = list(temp[i])
for j in range(len(temp[0])):
temp[i][j] = list(temp[i][j])
grid.extend(temp)
for i in grid:
for j in i:
print(j)
print()
输出是:
[1, 2, 3]
[2, 3, 1]
[3, 1, 2]
[1, 2, 3]
[3, 1, 2]
[2, 3, 1]
[2, 3, 1]
[1, 2, 3]
[3, 1, 2]
[2, 3, 1]
[3, 1, 2]
[1, 2, 3]
[3, 1, 2]
[1, 2, 3]
[2, 3, 1]
[3, 1, 2]
[2, 3, 1]
[1, 2, 3]
[3, 1, 2]
[2, 3, 1]
[1, 2, 3]
[3, 2, 1]
[2, 1, 3]
[1, 3, 2]
[1, 3, 2]
[3, 2, 1]
[2, 1, 3]
[1, 2, 3]
[3, 1, 2]
[2, 3, 1]
[2, 3, 1]
[1, 2, 3]
[3, 1, 2]
[2, 1, 3]
[1, 3, 2]
[3, 2, 1]
结果应该是这样的(顺序无关紧要):Latin Square
[1, 2, 3]
[2, 3, 1]
[3, 1, 2]
[1, 2, 3]
[3, 1, 2]
[2, 3, 1]
[1, 3, 2]
[2, 1, 3]
[3, 2, 1]
[1, 3, 2]
[3, 2, 1]
[2, 1, 3]
[2, 1, 3]
[1, 3, 2]
[3, 2, 1]
[2, 1, 3]
[3, 2, 1]
[1, 3, 2]
[2, 3, 1]
[1, 2, 3]
[3, 1, 2]
[2, 3, 1]
[3, 1, 2]
[1, 2, 3]
[3, 2, 1]
[1, 3, 2]
[2, 1, 3]
[3, 2, 1]
[2, 1, 3]
[1, 3, 2]
[3, 1, 2]
[1, 2, 3]
[2, 3, 1]
[3, 1, 2]
[2, 3, 1]
[1, 2, 3]
最佳答案
您可以使用带有生成器的递归:
def row(n, r, c = []):
if len(c) == n:
yield c
for i in range(1, n+1):
if i not in c and i not in r[len(c)]:
yield from row(n, r, c+[i])
def to_latin(n, c = []):
if len(c) == n:
yield c
else:
for i in row(n, [[]]*n if not c else list(zip(*c))):
yield from to_latin(n, c+[i])
<小时/>
for i in to_latin(3):
for b in i:
print(b)
print('-'*9)
输出:
[1, 2, 3]
[2, 3, 1]
[3, 1, 2]
---------
[1, 2, 3]
[3, 1, 2]
[2, 3, 1]
---------
[1, 3, 2]
[2, 1, 3]
[3, 2, 1]
---------
[1, 3, 2]
[3, 2, 1]
[2, 1, 3]
---------
[2, 1, 3]
[1, 3, 2]
[3, 2, 1]
---------
[2, 1, 3]
[3, 2, 1]
[1, 3, 2]
---------
[2, 3, 1]
[1, 2, 3]
[3, 1, 2]
---------
[2, 3, 1]
[3, 1, 2]
[1, 2, 3]
---------
[3, 1, 2]
[1, 2, 3]
[2, 3, 1]
---------
[3, 1, 2]
[2, 3, 1]
[1, 2, 3]
---------
[3, 2, 1]
[1, 3, 2]
[2, 1, 3]
---------
[3, 2, 1]
[2, 1, 3]
[1, 3, 2]
---------
关于python - 如何在 Python 中生成所有不同 3x3 拉丁方的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60880198/
我对UDF pig 拉丁有问题。 我正在尝试实现一个系统,该系统必须验证本地存储的矩阵与hadoop存储库中存储的一组矩阵之间是否存在“映射”。 对于映射,我的意思是在hadoop中是否存在一个存储矩
数据库的排序规则用法是什么?好吧,对于 HTML UTF-8,我知道一点,比如显示其他语言类型。但是对于数据库呢?我正在使用 latin-1(默认),我的 friend 告诉我改用 UTF。当我问为什
我有以下带有架构的数据 (t0: chararray,t1: int,t2: int) (B,4,2) (A,2,3) (A,3,2) (B,2,2) (A,1,2) (B,1,2) 我想生成以下结果
我正在尝试构建 AOSP 拉丁 IME(源代码:https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/m
我正在实现过滤器功能。我使用以下方法定义字符串是否适合查询: someString.IndexOf(someSearch, StringComparison.OrdinalIgnoreCase) >=
我是一名优秀的程序员,十分优秀!