1. 程式人生 > 其它 >numpy 中的陣列條件索引 where

numpy 中的陣列條件索引 where

技術標籤:Numpy,pythonnumpypythonpythonnumpy

import numpy as np
a = np.array([1,2,3,-1,-2,9])

where 的條件索引使用:

c = np.where(a==3)
print(c) # tuble
print(c[0])

結果:
在這裡插入圖片描述

ss = np.where(a>=1)
ss_value = a[ss]
print(ss[0]) # 對應的索引
print(ss_value) # 索引對應的值

結果:
在這裡插入圖片描述
在原陣列中修改值:

a[a>0] = 0  # 在原陣列上進行修改
print("change:"
,a)

結果:
在這裡插入圖片描述