Code example:
import numpy as np
a = np.zeros((5,5))
a[[0,1]] = 1
print('results with list based indexing\n', a)
a = np.zeros((5,5))
a[(0,1)] = 1
print('results with tuple based indexing\n',a)
Result:
results with list based indexing
[[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]]
results with tuple based indexing
[[ 0. 1. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]]
As you must have noticed, an index array with a list gave a different result than with a set of identical indices. I am using python3 with numpy version 1.13.3
What is the fundamental difference between indexing a numpy array with a list and tuple?
Tejas source
share