我现在有密码:
# 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将为您节省大量精力:
相关问题 更多 >
编程相关推荐