新闻资讯

带你了解Python中Numpy 的索引

带你了解Python中Numpy 的索引.png

如今,Python对大家来说是很熟悉的,今天就给大家详细介绍下PythonNumpy 的索引的相关信息。

[index]:一维索引

[row][col]:二维索引,也可写成[row,col]

 一维索引:

 import numpy as np

A = np.arange(3,15)

# array([3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])

 print(A[3])    # 6

 A = np.arange(3,15).reshape((3,4))

"""

array([[ 3,  4,  5,  6]

       [ 7,  8,  9, 10]

       [11, 12, 13, 14]])

"""

print(A[2])         

# [11 12 13 14]

二维索引:

 print(A[1][1])      # 8

print(A[1, 1])      # 8

print(A[1, 1:3])    # [8 9]

 # 使用for循环打印行

for row in A:

    print(row)

"""    

[ 3,  4,  5, 6]

[ 7,  8,  9, 10]

[11, 12, 13, 14]

"""

# 使用for循环打印列

for column in A.T:

    print(column)

"""  

[ 3,  7,  11]

[ 4,  8,  12]

[ 5,  9,  13]

[ 6, 10,  14]

"""

 # 迭代输出

print(A.flatten())   

# array([3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])

 for item in A.flat:

    print(item)

 # 3

# 4

……

# 14

 上述就是关于PythonNumpy 的索引的相关介绍了,希望能够给大家带来帮助。

文章部分内容源于网络,联系侵删*


客户经理
787116447 15252125410