我在squares.append的第一行得到def check_squares(grid):
squares=[]
count_dic={}
count_dic[0]=0
for num in range(1,10):
count_dic[num]=0
squares.append(list(((np.array(grid)[:3,:3]).reshape(-1))))
squares.append(list(((np.array(grid)[:3,3:6]).reshape(-1))))
squares.append(list(((np.array(grid)[:3,6:9]).reshape(-1))))
squares.append(list(((np.array(grid)[3:6,:3]).reshape(-1))))
squares.append(list(((np.array(grid)[3:6,3:6]).reshape(-1))))
squares.append(list(((np.array(grid)[3:6,6:9]).reshape(-1))))
squares.append(list(((np.array(grid)[6:9,:3]).reshape(-1))))
squares.append(list(((np.array(grid)[6:9,3:6]).reshape(-1))))
squares.append(list(((np.array(grid)[6:9,6:9]).reshape(-1))))
for lst in squares:
for i in lst:
count_dic[i]+=1
count_dic[0]=0
if (all(value <=1 for value in count_dic.values()))==True:
for num in range(1,10):
count_dic[num]=0
else:
return False
return True
ill_formed = [[5,3,4,6,7,8,9,1,2],
[6,7,2,1,9,5,3,4,8],
[1,9,8,3,4,2,5,6,7],
[8,5,9,7,6,1,4,2,3],
[4,2,6,8,5,3,7,9], # <---
[7,1,3,9,2,4,8,5,6],
[9,6,1,5,3,7,2,8,4],
[2,8,7,4,1,9,6,3,5],
[3,4,5,2,8,6,1,7,9]]
valid = [[5,3,4,6,7,8,9,1,2],
[6,7,2,1,9,5,3,4,8],
[1,9,8,3,4,2,5,6,7],
[8,5,9,7,6,1,4,2,3],
[4,2,6,8,5,3,7,9,1],
[7,1,3,9,2,4,8,5,6],
[9,6,1,5,3,7,2,8,4],
[2,8,7,4,1,9,6,3,5],
[3,4,5,2,8,6,1,7,9]]
print(check_squares(valid))
print (check_squares(ill-formed))
Error message: in check_squares
squares.append(list(((np.array(grid)[:3,:3]).reshape(-1))))
IndexError: too many indices for array
traceback.print_stack()
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 245, in <module>
main()
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 241, in main
kernel.start()
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 477, in start
ioloop.IOLoop.instance().start()
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "C:\ProgramData\Anaconda3\lib\site-packages\tornado\ioloop.py", line 888, in start
handler_func(fd_obj, events)
File "C:\ProgramData\Anaconda3\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "C:\ProgramData\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 235, in dispatch_shell
handler(stream, idents, msg)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 533, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2698, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2808, in run_ast_nodes
if self.run_code(code, result):
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-9-e4a4d877ed6a>", line 1, in <module>
traceback.print_stack()
,而IndexError上的其他帖子似乎有点太混乱了,所以如果可以有一个简单的解释那就太棒了为什么我得到这个!
{{1}}
非常感谢任何帮助,谢谢!
答案 0 :(得分:0)
这是我的解决方案 - 你可以尝试改进它。
def check_squares(grid):
grid = np.array(grid)
for i in grid:
unique , counts = (np.unique(i, return_counts=True))
#print(unique, counts)
if len(counts)<9:
return(False)
for i in np.split(grid,3):
for t in (np.hsplit(i,3)):
unique1 , counts1 = np.unique(t, return_counts=True)
#print(unique1,counts1)
if len(counts1)<9:
return(False)
return (True)
了解numpy数组拆分和切片。不要使用数组重塑六次来获得3 * 3子阵列。我使用过np split
和np.hsplit
在numpy空间中执行所有矩阵运算。
您可以将np.unique
的返回计数设置为True
,以获取输入中存在的唯一数字的数量以及发生的次数。您可以使用此获取来识别您的子阵列是否具有重复字符。
建议 - 目前你没有检查你的输入是否有效-eg:如果你的输入在sudoku网格中有12个,则代码不能处理它。
您还没有检查您的行和列是否是有效的数据列。我已经处理了行情况 - 我将离开检查每列是否是一个有效的数独列的工作
检查出来。快乐学习!