I've been experiencing some strange behavior with numpy arrays. Consider the code below:
import numpy as np
# 1
a = [np.arange(16)]
b = a
print(f'b = {b}')
# 2
b[0] = a[0][::2]
print(f'b = {b}')
# 3
b[0] = a[0][::2]
print(f'b = {b}')
b = [array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])]
b = [array([ 0, 2, 4, 6, 8, 10, 12, 14])]
b = [array([ 0, 4, 8, 12])]
I'd expect b in section 2 to be the same as the b in section 3, but for some reason it seems to be different. This doesn't seem to happen when a is a 1D array—writing b = a[::2] twice gives the same value for b. Why is this happening when a is a 2D array?
ais not a 2D array. You made a list with an array inside.b[0]to be reassigned to every other value ofa. Remember, you definedb = aso bothbandaare same lists. Just referenced.b = adoesn't make a copy.bandaare the same list. They're not arrays.a = np.arange(16)? Is there something fundamentally different between a list and an array?