問題の定義通り、縦に加算した値を見ていくのでは駄目ですか?
とりあえず縦に複数の0は出現しないと想定。
python
1data = [[8,7,6,5],
2 [1,0,3,4],
3 [16,15,14,13],
4 [9,10,0,12]]
5
6result = []
7for col in zip(*data):
8 if sum(col) == 34:
9 result.append(col)
10 else:
11 tmp = list(col)
12 tmp[tmp.index(0)] = 34 - sum(col)
13 result.append(tmp)
14
15print(list(map(list, zip(*result))))
16# => [[8, 7, 6, 5], [1, 2, 3, 4], [16, 15, 14, 13], [9, 10, 11, 12]]
zipでlistを転置する方法を利用しています。参考:
Python標準で転置行列
追記
作ってみました。上のコードを多少書き換えただけ。ヘンなデータが入ってきたときのことはまったく考えていません。
python
1data1 = [[8,7,6,5],
2 [1,0,3,4],
3 [16,15,14,13],
4 [9,10,0,12]]
5
6data2 = [[8,7,6,5],
7 [1,2,0,4],
8 [16,15,14,13],
9 [9,10,0,12]]
10
11def f(data):
12 result = []
13 for col in zip(*data):
14 if sum(col) == 34:
15 result.append(col)
16 else:
17 if col.count(0) == 1:
18 # 一列に0が1つのとき
19 tmp = list(col)
20 tmp[tmp.index(0)] = 34 - sum(col)
21 result.append(tmp)
22 else:
23 # 一列に2つあるとき
24 fillvals = [i for i in range(1, 17) if i not in sum(data, [])]
25 result.append([fillvals.pop() if e == 0 else e for e in col])
26
27 return list(map(list, zip(*result)))
28
29print(f(data1))
30print(f(data2))
31""" =>
32[[8, 7, 6, 5], [1, 2, 3, 4], [16, 15, 14, 13], [9, 10, 11, 12]]
33[[8, 7, 6, 5], [1, 2, 11, 4], [16, 15, 14, 13], [9, 10, 3, 12]]
34"""