热线电话:13121318867

登录
2018-12-18 阅读量: 778
numpy怎么索引数组?

数组索引:了解数组索引的基础知识对于分析和操作数组对象非常重要。NumPy提供了许多方法来进行数组索引。

  • 切片:就像python中的列表一样,可以对NumPy数组进行切片。由于数组可以是多维的,因此您需要为数组的每个维指定一个切片。
  • 整数数组索引:在此方法中,传递列表以便为每个维度建立索引。完成对应元素的一对一映射以构造新的任意数组。
  • 布尔数组索引:当我们想从数组中选择满足某些条件的元素时,使用此方法
  • # Python program to demonstrate
  • # indexing in numpy
  • import numpy as np
  • # An exemplar array
  • arr = np.array([[-1, 2, 0, 4],
  • [4, -0.5, 6, 0],
  • [2.6, 0, 7, 8],
  • [3, -7, 4, 2.0]])
  • # Slicing array
  • temp = arr[:2, ::2]
  • print ("Array with first 2 rows and alternate"
  • "columns(0 and 2):\n", temp)
  • # Integer array indexing example
  • temp = arr[[0, 1, 2, 3], [3, 2, 1, 0]]
  • print ("\nElements at indices (0, 3), (1, 2), (2, 1),"
  • "(3, 0):\n", temp)
  • # boolean array indexing example
  • cond = arr > 0 # cond is a boolean array
  • temp = arr[cond]
  • print ("\nElements greater than 0:\n", temp)

0.0000
7
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子