Issue
I am trying to apply a function to a two dimensional array, however I want the output to be a two dimensional array as well, I want it to preserve the same stucture after each element is transformed by the function. I have created the following simple case, however my output is:
[101, 103, 104, 105, 105, 104, 198, 199, 948, 132, 106, 106]
instead of a two dimensional array of the same shape as the original. Here follows the code so that you can replicate:
Vamos = [
[2, 4, 5, 6],
[6, 5, 99,100],
[849, 33, 7, 7]
]
Vamos1=[]
for j in range(len(Vamos)):
for i in range(len(Vamos[1])):
Vamos2=(Vamos[j][i])+99
Vamos1.append(Vamos2)
Vamos1
Thanks in advance.
Solution
Python isn't magically going to split your list into 2D if you throw all the items into it in a double loop.
Vamos = [
[2, 4, 5, 6],
[6, 5, 99,100],
[849, 33, 7, 7]
]
Vamos1=[]
for j in range(len(Vamos)):
row = []
for i in range(len(Vamos[1])):
Vamos2=(Vamos[j][i])+99
row.append(Vamos2)
Vamos1.append(row)
print(Vamos1)
Answered By - matszwecja
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.