我开始学习 Python,我只是从一个简单的例子开始。问题是计算表中每个位置附近的地雷。考虑下面的输入文件:
4 4
*...
....
.*..
....
3 5
**...
.....
.*...
0 0
输出应该像
Field #1:
*100
2210
1*10
1110
Field #2:
**100
33200
1*100
我的代码是:
#!/usr/bin/python
import pprint
fin = open("1.2.in")
fout = open("1.2.out")
while True:
i, j = [int(x) for x in fin.readline().split()]
if(i == 0):
break
arr = []
for k in range(0,i):
line = fin.readline();
arr.append(list(line))
pprint.pprint(arr)
resarr = [[0]*j]*i
for row in range(0,i):
for col in range(0,j):
for rowIndex in range(-1,2):
for colIndex in range(-1,2):
# print row,rowIndex, col,colIndex
if (row + rowIndex < i) and (row + rowIndex >= 0) and ( col + colIndex < j) and (col+colIndex >=0) and (rowIndex != 0 or colIndex != 0):
#pprint.pprint(resarr[row][col])
#if arr[row+rowIndex][col+colIndex] == "*":
#print row,rowIndex, col,colIndex, " ", arr[row+rowIndex][col+colIndex]
#print resarr[row][col]
resarr[row][col] += 1
#pprint.pprint(resarr)
# print col+colIndex
print i,j
pprint.pprint(resarr)
我不知道出了什么问题,但是当我想递增时resarr
,总列会递增。