如何在 "for loop" 中插入不同的列表切片?
How do I insert different list slice in "for loop"?
我正在尝试为大小为 9x9 的 numpy 数组的每一行、每一列和每一字段创建一个字典。 (就像在数独游戏中一样)能够查看是否没有重复项。一切正常,但我想知道是否可以在循环中创建 field_dic
?:
sudoku =[] # Some 9x9 numpy.ndarray
row_dic = {}
col_dic = {}
field_dic = {}
for i in range(9):
col_dic[f"Row{i}"] = dict(Counter(sudoku[:, i]))
row_dic[f"Col{i}"] = dict(Counter(sudoku[i, :]))
field_dic = {
"Field0": dict(Counter(list(sudoku[:3, :3].flat))),
"Field1": dict(Counter(list(sudoku[:3, 3:6].flat))),
"Field2": dict(Counter(list(sudoku[:3, 6:].flat))),
"Field3": dict(Counter(list(sudoku[3:6, :3].flat))),
"Field4": dict(Counter(list(sudoku[3:6, 3:6].flat))),
"Field5": dict(Counter(list(sudoku[3:6, 6:].flat))),
"Field6": dict(Counter(list(sudoku[6:, :3].flat))),
"Field7": dict(Counter(list(sudoku[6:, 3:6].flat))),
"Field8": dict(Counter(list(sudoku[6:, 6:].flat)))}
您可以使用循环,但听写理解可能是更好的选择。
让您抢先一步:
import itertools
ind_groups = [(0, 3), (3, 6), (6, 9)]
field_positions = itertools.product(ind_groups, ind_groups)
field_dict = {
f'Field{i}': (r0, r1, c0, c1) for (i, ((r0, r1), (c0, c1))) in enumerate(field_positions)
}
您 运行 将原始数组从 (9,9)
重塑为 (3,3,3,3)
。然后,重塑数组的维度 0
将为您提供行,维度 2
将为您提供块的列位置。
import numpy as np
sample = np.arange(81).reshape((9,9))
reshaped = sample.reshape(3,3,3,3)
field_dic = {}
for i in range(9):
field_dic[f"Field{i}"] = reshaped[(i//3),:,i%3,:].reshape(9)
结果如下:
>>> print('first block:\n', reshaped[0,:,0,:])
first block:
[[ 0 1 2]
[ 9 10 11]
[18 19 20]]
>>> print(field_dic['Field0'])
[ 0 1 2 9 10 11 18 19 20]
>>> print(field_dic['Field1'])
[ 3 4 5 12 13 14 21 22 23]
>>> print(field_dic['Field3'])
[27 28 29 36 37 38 45 46 47]
我正在尝试为大小为 9x9 的 numpy 数组的每一行、每一列和每一字段创建一个字典。 (就像在数独游戏中一样)能够查看是否没有重复项。一切正常,但我想知道是否可以在循环中创建 field_dic
?:
sudoku =[] # Some 9x9 numpy.ndarray
row_dic = {}
col_dic = {}
field_dic = {}
for i in range(9):
col_dic[f"Row{i}"] = dict(Counter(sudoku[:, i]))
row_dic[f"Col{i}"] = dict(Counter(sudoku[i, :]))
field_dic = {
"Field0": dict(Counter(list(sudoku[:3, :3].flat))),
"Field1": dict(Counter(list(sudoku[:3, 3:6].flat))),
"Field2": dict(Counter(list(sudoku[:3, 6:].flat))),
"Field3": dict(Counter(list(sudoku[3:6, :3].flat))),
"Field4": dict(Counter(list(sudoku[3:6, 3:6].flat))),
"Field5": dict(Counter(list(sudoku[3:6, 6:].flat))),
"Field6": dict(Counter(list(sudoku[6:, :3].flat))),
"Field7": dict(Counter(list(sudoku[6:, 3:6].flat))),
"Field8": dict(Counter(list(sudoku[6:, 6:].flat)))}
您可以使用循环,但听写理解可能是更好的选择。
让您抢先一步:
import itertools
ind_groups = [(0, 3), (3, 6), (6, 9)]
field_positions = itertools.product(ind_groups, ind_groups)
field_dict = {
f'Field{i}': (r0, r1, c0, c1) for (i, ((r0, r1), (c0, c1))) in enumerate(field_positions)
}
您 运行 将原始数组从 (9,9)
重塑为 (3,3,3,3)
。然后,重塑数组的维度 0
将为您提供行,维度 2
将为您提供块的列位置。
import numpy as np
sample = np.arange(81).reshape((9,9))
reshaped = sample.reshape(3,3,3,3)
field_dic = {}
for i in range(9):
field_dic[f"Field{i}"] = reshaped[(i//3),:,i%3,:].reshape(9)
结果如下:
>>> print('first block:\n', reshaped[0,:,0,:])
first block:
[[ 0 1 2]
[ 9 10 11]
[18 19 20]]
>>> print(field_dic['Field0'])
[ 0 1 2 9 10 11 18 19 20]
>>> print(field_dic['Field1'])
[ 3 4 5 12 13 14 21 22 23]
>>> print(field_dic['Field3'])
[27 28 29 36 37 38 45 46 47]