當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python numpy.select()用法及代碼示例

numpy.select()()函數根據條件返回從選擇列表中的元素繪製的數組。

用法: numpy.select(condlist, choicelist, default = 0)
參數:
condlist :[list of bool ndarrays] It determine from which array in choicelist the output elements are taken. When multiple conditions are satisfied, the first one encountered in condlist is used.
choicelist :[list of ndarrays] The list of arrays from which the output elements are taken. It has to be of the same length as condlist.
default :[scalar, optional] The element inserted in output when all conditions evaluate to False.
Return :[ndarray] An array drawn from elements in choicelist, depending on conditions.

代碼1:

# Python program explaining 
# numpy.select() function 
  
# importing numpy as geek  
import numpy as geek 
  
arr = geek.arange(8) 
  
condlist = [arr<3, arr>4] 
choicelist = [arr, arr**3] 
  
gfg = geek.select(condlist, choicelist) 
  
print (gfg)

輸出:

[  0, 1, 2, 0, 0, 125, 216, 343]


代碼2:

# Python program explaining 
# numpy.select() function 
  
# importing numpy as geek  
import numpy as geek 
  
arr = geek.arange(8) 
  
condlist = [arr<4, arr>6] 
choicelist = [arr, arr**2] 
  
gfg = geek.select(condlist, choicelist) 
  
print (gfg)

輸出:

[ 0, 1, 2, 3, 0, 0, 0, 49]

相關用法


注:本文由純淨天空篩選整理自sanjoy_62大神的英文原創作品 numpy.select() function | Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。