如果只想直接访问一维numpy数组中特定位置的元素,可以使用
一维索引
.
例如:
我想访问3x3数组第一行第三列中的元素
C
,那么我会的
C[0,2]
.
c = np.random.rand( 3,3 )
print(c)
print( 'Element:', c[0,2])
检查官方文件
Numpy Indexing
_更新__
对于元组列表,应为每个数据结构编制索引。
import numpy as np
a =[
( np.random.rand( 2,2 ), 0 ), #first tuple
( np.random.rand( 2,2 ), 2 ), #second tuple
( np.random.rand( 2,2 ), 3 ), # ...
( np.random.rand( 2,2 ), 1 )
]
print( np.shape(a) ) # accessing list a
# (4,2)
print( np.shape(a[0]) ) # accessing the first tuple in a
# (2)
print( np.shape(a[0][0]) ) # accessing the 2x2 array inside the first tuple
# (2,2)
print( np.shape(a[0][0][0,1]) ) # accessing the [0,1] element inside the array
# ()
#another example
c = ( np.array([ [1,2,3],[4,5,6],[7,8,9] ]), 8 )
print( c[0][0,2] ) # output: 3