Jul*_*n M -1 python arrays numpy filter
你能帮我以更简洁的 Python 形式并可能使用 numpy 简化以下表达式吗?在评估值是否介于小数和大数之间时,内置的 numpy.argwhere() 未按预期工作。
myList = [4.2, 6.0, 10.2]
low = 10
high = 11
indicies = []
for i, v in enumerate(myList):
if low < v < high:
indicies.append(i)
Run Code Online (Sandbox Code Playgroud)
在下面的 np.argwhere 中:
import numpy as np
myList = np.array([4.2, 6.0, 10.2])
low = 10
high = 11
indiciesWorking = np.argwhere(low < myList) # working
indiciesError = np.argwhere(low < myList < high) # ValueError
Run Code Online (Sandbox Code Playgroud)
这返回
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Run Code Online (Sandbox Code Playgroud)
你需要分成low < myList < high两个条件
theta = np.array([10.2, 6.0, 10.2])
indices = np.argwhere((theta > low) & (theta < high))
Run Code Online (Sandbox Code Playgroud)
这将为您提供一个ndarray [[0] [2]],您可以使用 更改为一维数组[:,0],或者直接where使用
indices = np.where((theta > low) & (theta < high))[0]
print(indices) # [0 2]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1303 次 |
| 最近记录: |