在矩阵元素邻居中找到总和

时间:2014-11-30 10:05:32

标签: python python-3.x matrix

===已解决===

我需要找到他的邻居的矩阵和的每个元素并将其写入另一个矩阵。 例如: matrix[0][0]

matrix2[0][0] = matrix[-1][0] + matrix[0][-1] + matrix[1][0] + matrix[0][1]

1 2 3
3 4 5
5 6 7

表示[0] [0] - 2 + 3 + 3 + 5

输入矩阵:

matrix = []
tmp = []
answer = []
inp = ''
while inp != 'end':
  tmp = [int(i) for i in input().split() if i != 'end']
  if len(tmp) == 0: inp = 'end'
  else: matrix.append(tmp)
  tmp = []

现在我不知道完成此任务需要哪个“if ... else”条件。

当前代码:

for i in range(0, len(matrix)):
  for j in range(0, len(matrix[i])):
    summ = 0
    if i-1 >= 0:
      summ += matrix[i-1][j]
    elif i-1 < 0:
      summ += matrix[-1][j]

    if i+1 > len(matrix)-1:
      summ += matrix[0][j]
    elif i+1 <= len(matrix)-1:
      summ += matrix[i+1][j]

    if j-1 >= 0:
      summ += matrix[i][j-1]  
    elif j-1 < 0:
      summ += matrix[i][-1]

    if j+1 > len(matrix[i])-1:
      summ += matrix[i][0]
    elif j+1 <= len(matrix[i])-1:
      summ += matrix[i][j+1]
    answer[i][j] = summ
    continue

有人可以指出我的代码有什么问题吗?

解决:

   matrix = []
    tmp = []
    answer = []
    inp = ''
    flg = 1
    while inp != 'end':
      tmp = [int(i) for i in input().split() if i != 'end']
      if len(tmp) == 0: inp = 'end'
      else: matrix.append(tmp)
      tmp = []
    tmp = []
    for i in range(0, len(matrix)):
      if len(matrix)==1 and len(matrix[i])==1:
        summ = matrix[0][0] *4
        flg = 0
        answer.append(summ)
        break
      for j in range(0, len(matrix[i])):
        summ = 0

        if i-1 >= 0:
          summ += matrix[i-1][j]
        elif i-1 < 0:
          summ += matrix[-1][j]

        if i+1 > len(matrix)-1:
          summ += matrix[0][j]
        elif i+1 <= len(matrix)-1:
          summ += matrix[i+1][j]

        if j-1 >= 0:
          summ += matrix[i][j-1]
        elif j-1 < 0:
          summ += matrix[i][-1]

        if j+1 > len(matrix[i])-1:
          summ += matrix[i][0]
        elif j+1 <= len(matrix[i])-1:
          summ += matrix[i][j+1]

        tmp.append(summ)
        continue
      answer.append(tmp)
      tmp = []

    i=j=0
    if flg:
      for i in range(0, len(answer)):
        for j in range(0, len(answer[i])):
          print(answer[i][j], ' ', end='')
        print('')
    if not flg:
      print(summ)

2 个答案:

答案 0 :(得分:1)

这样的事情:

>>> a = [[1,2,3],[3,4,5],[5,6,7]]
>>> a
[[1, 2, 3],
 [3, 4, 5],
 [5, 6, 7]]
>>> def neighbors(i,j,a):
...     return [a[i][j-1], a[i][(j+1)%len(a[0])], a[i-1][j], a[(i+1)%len(a)][j]]
...
>>> [[sum(neighbors(i,j,a)) for j in range(len(a[0]))] for i in range(len(a))]
[[13, 14, 15],
 [15, 16, 17],
 [17, 18, 19]]
>>> a.append([100, 200, 300])
>>> a
[[1, 2, 3],
 [3, 4, 5],
 [5, 6, 7],
 [100, 200, 300]]
>>> [[sum(neighbors(i,j,a)) for j in range(len(a[0]))] for i in range(len(a))]
[[108, 208, 308],
 [15, 16, 17],
 [116, 216, 316],
 [506, 408, 310]]

答案 1 :(得分:0)

>>> a=[[1,2,3],[3,4,5],[5,6,7]]
>>> b=[]
>>> for i in range(len(a)):
...    tmp=[]
...    for j in range(len(a[0])):
...       tmp.append(a[(i+1)%len(a)][j]+a[(i-1)%len(a)][j]+a[i][(j+1)%len(a[0])]+a[i][(j-1)%len(a[0])])
...    b.append(tmp)
... 
>>> b
[[13, 14, 15], [15, 16, 17], [17, 18, 19]]