0

数独に似た行で次のデータを出力する方法

p = [
    [-8, 0,  0,  0,  0,  0,   0,  0, 0],
    [0,  0, -3, -6,  0,  0,   0,  0, 0],
    [0, -7,  0,  0, -9,  0,  -2,  0, 0],
    [0, -5,  0,  0,  0, -7,   0,  0, 0],
    [0,  0,  0,  0, -4, -5,  -7,  0, 0],
    [0,  0,  0, -1,  0,  0,   0, -3, 0],
    [0,  0, -1,  0,  0,  0,   0, -6, -8],
    [0,  0, -8, -5,  0,  0,   0,  -1, 0],
    [0, -9,  0,  0,  0,  0,  -4,   0, 0]]
print ("Unsolved:")

w="\n".join(re.findall("(?p).{,9}", p))[:-1]
print (w)

次の方法で出力を取得するには

0 | 0 | 3 | 0 | 2 | 0 | 6 | 0 | 0
9 | 0 | 0 | 3 | 0 | 5 | 0 | 0 | 1
0 | 0 | 1 | 8 | 0 | 6 | 4 | 0 | 0
0 | 0 | 8 | 1 | 0 | 2 | 9 | 0 | 0
7 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 8
0 | 0 | 6 | 7 | 0 | 8 | 2 | 0 | 0
0 | 0 | 2 | 6 | 0 | 9 | 5 | 0 | 0
8 | 0 | 0 | 2 | 0 | 3 | 0 | 0 | 9
0 | 0 | 5 | 0 | 1 | 0 | 3 | 0 | 0
4

1 に答える 1

3
rows = ("|".join(map(" {:2d} ".format, row)) for row in p)
print("\n".join(rows))

出力

-8 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
  0 | 0 | -3 | -6 | 0 | 0 | 0 | 0 | 0
  0 | -7 | 0 | 0 | -9 | 0 | -2 | 0 | 0
  0 | -5 | 0 | 0 | 0 | -7 | 0 | 0 | 0
  0 | 0 | 0 | 0 | -4 | -5 | -7 | 0 | 0
  0 | 0 | 0 | -1 | 0 | 0 | 0 | -3 | 0
  0 | 0 | -1 | 0 | 0 | 0 | 0 | -6 | -8
  0 | 0 | -8 | -5 | 0 | 0 | 0 | -1 | 0
  0 | -9 | 0 | 0 | 0 | 0 | -4 | 0 | 0
于 2012-09-28T14:42:45.780 に答える