distinguish "list" & "array" in python
list
& array
It seems array
is a subconcept of numpy
package. list
& array
are with some different properties.
The most apparent difference is lists do concatenate and arrays do math which is shown as follow.
import numpy as np
list1 = [1, 2]
list2 = [3,4]
type(list1) # key point
Out[26]: list
list1 + list2 # key point
Out[27]: [1, 2, 3, 4]
array1 = np.array([1,2])
array2 = np.array([3,4])
type(array1) # key point
Out[31]: numpy.ndarray
array1 + array2 # key point
Out[32]: array([4, 6])
what is aarray[None,0]
for
It makes me comfused that what the grammer aarray[None,0]
stands for. After some trying, I thought I found the meaning. For a 2-dimensional array, aarray[None,0]
retrun aarray[0]
in 2 dimensional space, the length of one dimension of which is 1.
Differences show as below:
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print(type(a))
print('a[0] ============',a[0])
print('a[:,0] ============',a[:,0])
print('a[None,0] ============',a[None,0])
print('a[0][None,:] ============',a[0][None,:])
output:
<class 'numpy.ndarray'>
a[0] ============ [1 2 3]
a[:,0] ============ [1 4]
a[None,0] ============ [[1 2 3]]
a[0][None,:] ============ [[1 2 3]]