- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前有代码:
# gets all the horizontal rows from file
rows = [line.strip('\n') for line in open("F:/sudoku practice.txt",'r')]
# gets all the vertical columns from horizontal rows
columns = [(''.join(list(line[i]for line in rows)))for i in range(8)]
import itertools
sections = [[],[],[],[],[],[],[],[],[]]
for line in rows[:3]:
sections[0].append([(''.join(line[:3]))])
sections[1].append([(''.join(line[3:6]))])
sections[2].append([(''.join(line[6:9]))])
for line in rows[3:6]:
sections[3].append([(''.join(line[:3]))])
sections[4].append([(''.join(line[3:6]))])
sections[5].append([(''.join(line[6:9]))])
for line in rows[6:9]:
sections[6].append([(''.join(line[:3]))])
sections[7].append([(''.join(line[3:6]))])
sections[8].append([(''.join(line[6:9]))])
# flattening the list of lists of lists into a list
for i in range(9):
sections[i] = ''.join(list(itertools.chain.from_iterable(sections[i])))
print(sections)
返回:
[' 2 457689', '456 8 237', '789236 4 ', ' 5274396', '362 9 574', '9746538 ', ' 4 761938', '618 4 725', '397528 6 ']
列表中的每个项目代表数独谜题的一部分,这正是我想要的,但我希望有一种更短的方法来获取所有部分,我有一个用于获取行和列的衬垫并且如果可能的话想要类似的东西
最佳答案
因为在数独游戏中,您需要大量访问子数组和对角线等,Numpy将为您节省很多精力:
import numpy as np
a = np.random.randint(1, 9, (9,9))
print a
[[2 6 3 2 3 4 2 6 5]
[5 3 7 4 5 2 3 4 7]
[4 3 1 8 2 7 4 4 2]
[4 6 5 5 3 6 2 6 8]
[3 8 3 5 5 4 5 7 3]
[4 6 2 3 1 6 1 4 2]
[7 2 6 7 8 3 6 6 3]
[8 1 7 7 5 7 5 2 1]
[7 5 3 3 6 1 3 4 2]]
# middle section
print a[3:6,3:6]
[[5 3 6]
[5 5 4]
[3 1 6]]
# lower middle diagonals
print a[6:, 3:6].diagonal()
[7 5 1]
# lower middle upward diagonals
print a[6:, 3:6][::-1].diagonal()
[3 5 3]
关于python - 一种从水平行列表中获取部分的简短方法,用于数独解谜器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31076174/
void main(){ char c; unsigned char uc; unsigned short us1, us2; short s1, s2; c
我要删除 wordpress html 格式在 woocommerce 产品简短描述中。添加 电话 到处标记。我知道如何在 wp 帖子和页面中做到这一点 remove_filter( 'the_exc
我通常会打开一个命令的联机帮助页,该命令已经知道我正在搜索的选项并阅读其描述。有时只需搜索该选项即可立即生效,有时该选项在其他地方被引用,有时该选项仅作为子字符串出现在前面的文本中。 作为一个具体的例
假设我们有一个编号的圆圈。我们想从 A 点到 B 点,但不知道应该向左还是向右。你会如何使用数字来计算你应该朝哪个方向前进? 示例: 我们目前在 1。我们想继续 5。我可以直观地看到 5 更近,所以我
我正在使用以下内容将产品名称和价格添加到悬停框。 在 functions.php 中 // Alter produt loop individual products add_action( 'woo
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我有一个for循环 for ($x=1; $x<=5; $x++){ ($x == 3)? continue : true; //some code here } 现在执行时出现错误
我刚刚开始我的第一个 Java Swing 项目(之前主要做基于 Web 的应用程序),并尝试了解如何构建一个适当的架构,并在 MVC 组件之间分离关注点。 我发现的几乎所有文档都非常深入地介绍了每个
我是一名优秀的程序员,十分优秀!