2D array append

Asked 1 years ago, Updated 1 years ago, 415 views

A1 = [[6,-8,11], [7,10,12],[12,14,7],[-5, 9, 4],[5, 7, 8],[3, 13, -2],[11, 8, 9],[3, 5, 4]]

I'd like to add the sum of the first and third values after this

[[6, -8, 11, 17],[7, 10, 12, 19],[12, 14, 7, 19],[-5, 9, 4, -1],[5, 7, 8, 13], [3, 13, -2, 1], [11, 8, 9, 20], [3, 5, 4, 7]]

How do I do this?

A1 = [[6,-8,11], [7,10,12],[12,14,7],[-5, 9, 4],[5, 7, 8],[3, 13, -2],[11, 8, 9],[3, 5, 4]]

def appendW(A):

  return w

w1 = appendW(A1)
w1

I want to print it out like this.

array append

2022-10-30 19:06

1 Answers

A1 = [[6,-8,11], [7,10,12],[12,14,7],[-5, 9, 4],[5, 7, 8],[3, 13, -2],[11, 8, 9],[3, 5, 4]]

for n, i in enumerate(A1):
    ii = i[0] + i[2]
    A1[n].append(ii)

print(A1)
>> [[6, -8, 11, 17], [7, 10, 12, 19], [12, 14, 7, 19], [-5, 9, 4, -1], [5, 7, 8, 13], [3, 13, -2, 1], [11, 8, 9, 20], [3, 5, 4, 7]]


2022-10-30 19:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.