I am using a deque
to store data that is going to be processed. The processing only starts when the deque is full so in a first step I fill my buffer the following way:
JavaScript
x
7
1
from collections import deque
2
import numpy as np
3
4
my_deque = deque(maxlen=10)
5
dims = (20, 20)
6
my_deque.extend([np.ones(dims)] * 10)
7
However, when I do this and I modify one of the elements of my_deque
, all elements of my_deque
are modified.
One alternative I found to avoid this issue is to initialize my deque
the following way
JavaScript
1
3
1
for i in range(10):
2
my_deque.append(np.ones(dims))
3
Is there a more elegant way to achieve this?
Edit: One example of modification that create the issue is the following
JavaScript
1
2
1
my_deque[7][2, :] = 0
2
After this line of code, the third row of every element of my_deque
is a row of zeros, not just the one at index 7
Advertisement
Answer
@MichaelSzczesny’s comment was the right answer, it seems to have been deleted so I post it here:
JavaScript
1
6
1
from collections import deque
2
import numpy as np
3
4
my_deque = deque(maxlen=10)dims = (10, 20, 20)
5
my_deque.extend(np.ones(dims))
6