I want to append each element of
[1,2]
to[[1], [2], [3]]
and as a consequence, the final array that I want is[[1,1], [1,2], [2,1], [2,2], [3,1], [3,2]]
But my code has a mistake I couldn’t recognize it yet, and the result of the python code below is
[[1, 1, 2], [1, 1, 2], [2, 1, 2], [2, 1, 2], [3, 1, 2], [3, 1, 2]]
The python code:
JavaScript
x
18
18
1
tor=[]
2
arr=[1,2]
3
arz=[[1], [2], [3]]
4
5
each=0
6
while each<len(arz):
7
8
eleman=arz[each]
9
index=0
10
while index < len(arr):
11
k=arr[index]
12
eleman=arz[each]
13
eleman.append(k)
14
tor.append(eleman)
15
index = index+1
16
17
each=each+1
18
Advertisement
Answer
it would be eleman=arz[each].copy()
as lists are mutable so every time you change an element in the original list it will get reflected in the resultant array