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.

如果二維數(shù)組數(shù)組使用where的話返回的也是一個二維數(shù)組,準(zhǔn)確的來說一維數(shù)組返回的也是一個二維數(shù)組

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列大于五

關(guān)于一維數(shù)組的返回值,這樣表示

image.png
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,890評論 0 33
  • 先決條件 在閱讀這個教程之前,你多少需要知道點python。如果你想從新回憶下,請看看Python Tutoria...
    舒map閱讀 2,717評論 1 13
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • NumPy是Python中關(guān)于科學(xué)計算的一個類庫,在這里簡單介紹一下。 來源:https://docs.scipy...
    灰太狼_black閱讀 1,329評論 0 5
  • 對于一個剛剛進去職場的新人來說,如果能遇到一個“職場導(dǎo)師”,我覺得也就是一個好的boss,能少走很多彎路。 也許每...
    沒什么特殊癖好閱讀 394評論 0 0

友情鏈接更多精彩內(nèi)容