Numpy
HowTo
View of Element
Draft for Information Only
Content
Return a view of element of a ndarray Examples of Return Element of a (3,) ndarray Examples of Return Element of a (2, 3) ndarray Examples of Return Element of a (3, 3, 3) ndarray Sources and References
Return a view of element of a ndarray
Examples of Return Element of a (3,) ndarray
Return Element of a (3,) ndarray Python Code Input:Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AM
D64)] on win32
>>> import numpy as np
>>> a=np.array([0,1,2])
>>> a
array([0, 1, 2])
>>> a.shape
(3,)
>>> a[0]
0
>>> a[1]
1
>>> a[0],a[1]
(0, 1)
>>> a[0:0]
array([], dtype=int32)
>>> a[0:1]
array([0])
>>> a[1:2]
array([1])
>>> a[0:2]
array([0, 1])
>>> a[0::2]
array([0, 2])
>>>
Examples of Return Element of a (2, 3) ndarray
Return Element of a (2, 3) ndarray Python Code Input:Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AM
D64)] on win32
>>> import numpy as np
>>> a=np.array([[0,1,2],[3,4,5]])
>>> a
array([[0, 1, 2],
[3, 4, 5]])
>>> a.shape
(2, 3)
>>> a[0]
array([0, 1, 2])
>>> a[1]
array([3, 4, 5])
>>> a[0][0]
0
>>> a[0][1]
1
>>> a[0,0]
0
>>> a[1,1]
4
>>> a[0,0:1]
array([0])
>>> a[1,1:2]
array([4])
>>> a[:,0]
array([0, 3])
>>> a[:,1]
array([1, 4])
>>> a[:,1:2]
array([[1]
[4]])
>>> a[:1,1:]
array([[1, 2]])
>>> a[1:,1:]
array([[4, 5]])
>>> a[1:,:-1]
array([[3, 4]])
>>> a[:1,:-1]
array([[0, 1]])
>>> a[:,1:]
array([[1, 2],
[4, 5]])
>>> a[:,:-1]
array([[0, 1],
[3, 4]])
>>> a[:,:]
array([[0, 1, 2],
[3, 4, 5]])
>>>
Examples of Return Element of a (3, 3, 3) ndarray
Return Element of a (3, 3, 3) ndarray Python Code Input:Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AM
D64)] on win32
>>> import numpy as np
>>> a=np.array([[[0,1,2],[3,4,5],[6,7,8]],
... [[10,11,12],[13,14,15],[16,17,18]],
... [[20,21,22],[23,24,25],[26,27,28]]])
>>> a
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[10, 11, 12],
[13, 14, 15],
[16, 17, 18]],
[[20, 21, 22],
[23, 24, 25],
[26, 27, 28]]])
>>> a.shape
(3, 3, 3)
>>> a[0]
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> a[0][0]
array([0, 1, 2])
>>> a[0][0][0]
0
>>> a[0,0,0]
0
>>> a[0,0,0:1]
array([0])
>>> a[0,0,0::2]
array([0, 2])
>>> a[0,0,:]
array([0, 1, 2])
>>> a[0,0]
array([0, 1, 2])
>>> a[0,:,:]
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> a[0,:-1,:-1]
array([[0, 1],
[3, 4]])
>>> a[0,1:,1:]
array([[4, 5],
[7, 8]])
>>> a[:,1:,1:]
array([[[ 4, 5],
[ 7, 8]],
[[14, 15],
[17, 18]],
[[24, 25],
[27, 28]]])
>>> a[:,1:,:]
array([[[ 3, 4, 5],
[ 6, 7, 8]],
[[13, 14, 15],
[16, 17, 18]],
[[23, 24, 25],
[26, 27, 28]]])
>>> a[:,1:2,:]
array([[[ 3, 4, 5]],
[[13, 14, 15]],
[[23, 24, 25]]])
>>>
Sources and References
https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists
©sideway
ID: 220100013 Last Updated: 1/13/2022 Revision: 0
|
|