python|numpy where的使用 ,返回值的使用

numpy.where
numpy.
where
(condition[, x, y])
Return elements, either from x or y, depending on condition.
If only condition is given, return condition.nonzero()
.

Returns:
out : ndarray or tuple of ndarrays
If both x and y are specified, the output array contains elements of xwhere condition is True, and elements from y elsewhere.
If only condition is given, return the tuple condition.nonzero()
, the indices where condition is True.

如果二维数组数组使用where的话返回的也是一个二维数组,准确的来说一维数组返回的也是一个二维数组

x = np.arange(9.).reshape(3, 3)
>>> np.where( x > 5 )
    (array([2, 2, 2]), array([0, 1, 2]))
>>> x[np.where( x > 3.0 )]               # Note: result is 1D.
    array([ 4.,  5.,  6.,  7.,  8.])
>>> np.where(x < 5, x, -1)               # 值替换
    array([[ 0.,  1.,  2.],
           [ 3.,  4., -1.],
           [-1., -1., -1.]])
image.png

(array([2, 2, 2]), array([0, 1, 2]))

第三行的1,2,3列大于五

关于一维数组的返回值,这样表示

image.png
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,788评论 0 33
  • 先决条件 在阅读这个教程之前,你多少需要知道点python。如果你想从新回忆下,请看看Python Tutoria...
    舒map阅读 2,618评论 1 13
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,083评论 19 139
  • NumPy是Python中关于科学计算的一个类库,在这里简单介绍一下。 来源:https://docs.scipy...
    灰太狼_black阅读 1,256评论 0 5
  • 对于一个刚刚进去职场的新人来说,如果能遇到一个“职场导师”,我觉得也就是一个好的boss,能少走很多弯路。 也许每...
    没什么特殊癖好阅读 385评论 0 0