我有一个名为dist的距离数组。我想选择一个范围内的dist。

 dists[(np.where(dists >= r)) and (np.where(dists <= r + dr))]

但是,这只选择条件

 (np.where(dists <= r + dr))

如果我使用临时变量按顺序执行命令,它可以很好地工作。为什么上面的代码不能工作,我如何让它工作?


当前回答

在你的特殊情况下,最好的方法就是把你的两个标准改为一个标准:

dists[abs(dists - r - dr/2.) <= dr/2.]

它只创建了一个布尔数组,在我看来更容易阅读,因为它说,dist是dr还是r?(虽然我将重新定义r为感兴趣区域的中心,而不是起点,因此r = r + dr/2。)但这并没有回答你的问题。


你问题的答案是: 如果你只是想过滤掉不符合你的标准的元素,你实际上不需要where:

dists[(dists >= r) & (dists <= r+dr)]

因为&会给你一个elementwise和(括号是必须的)。

或者,如果你出于某种原因想要使用where,你可以这样做:

 dists[(np.where((dists >= r) & (dists <= r + dr)))]

原因: 它不起作用的原因是np。Where返回索引列表,而不是布尔数组。您正在尝试在两个数字列表之间获取和,其中当然没有您所期望的True/False值。如果a和b都是True值,那么a和b返回b。所以说像[0,1,2]和[2,3,4]这样的东西只会给你[2,3,4]。下面是它的实际情况:

In [230]: dists = np.arange(0,10,.5)
In [231]: r = 5
In [232]: dr = 1

In [233]: np.where(dists >= r)
Out[233]: (array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),)

In [234]: np.where(dists <= r+dr)
Out[234]: (array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12]),)

In [235]: np.where(dists >= r) and np.where(dists <= r+dr)
Out[235]: (array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12]),)

例如,您希望比较的只是布尔数组

In [236]: dists >= r
Out[236]: 
array([False, False, False, False, False, False, False, False, False,
       False,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True], dtype=bool)

In [237]: dists <= r + dr
Out[237]: 
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True, False, False, False, False, False,
       False, False], dtype=bool)

In [238]: (dists >= r) & (dists <= r + dr)
Out[238]: 
array([False, False, False, False, False, False, False, False, False,
       False,  True,  True,  True, False, False, False, False, False,
       False, False], dtype=bool)

现在你可以调用np。在组合布尔数组上:

In [239]: np.where((dists >= r) & (dists <= r + dr))
Out[239]: (array([10, 11, 12]),)

In [240]: dists[np.where((dists >= r) & (dists <= r + dr))]
Out[240]: array([ 5. ,  5.5,  6. ])

或者简单地使用花哨的索引用布尔数组索引原始数组

In [241]: dists[(dists >= r) & (dists <= r + dr)]
Out[241]: array([ 5. ,  5.5,  6. ])

其他回答

这应该可以工作:

dists[((dists >= r) & (dists <= r+dr))]

在你的特殊情况下,最好的方法就是把你的两个标准改为一个标准:

dists[abs(dists - r - dr/2.) <= dr/2.]

它只创建了一个布尔数组,在我看来更容易阅读,因为它说,dist是dr还是r?(虽然我将重新定义r为感兴趣区域的中心,而不是起点,因此r = r + dr/2。)但这并没有回答你的问题。


你问题的答案是: 如果你只是想过滤掉不符合你的标准的元素,你实际上不需要where:

dists[(dists >= r) & (dists <= r+dr)]

因为&会给你一个elementwise和(括号是必须的)。

或者,如果你出于某种原因想要使用where,你可以这样做:

 dists[(np.where((dists >= r) & (dists <= r + dr)))]

原因: 它不起作用的原因是np。Where返回索引列表,而不是布尔数组。您正在尝试在两个数字列表之间获取和,其中当然没有您所期望的True/False值。如果a和b都是True值,那么a和b返回b。所以说像[0,1,2]和[2,3,4]这样的东西只会给你[2,3,4]。下面是它的实际情况:

In [230]: dists = np.arange(0,10,.5)
In [231]: r = 5
In [232]: dr = 1

In [233]: np.where(dists >= r)
Out[233]: (array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),)

In [234]: np.where(dists <= r+dr)
Out[234]: (array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12]),)

In [235]: np.where(dists >= r) and np.where(dists <= r+dr)
Out[235]: (array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12]),)

例如,您希望比较的只是布尔数组

In [236]: dists >= r
Out[236]: 
array([False, False, False, False, False, False, False, False, False,
       False,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True], dtype=bool)

In [237]: dists <= r + dr
Out[237]: 
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True, False, False, False, False, False,
       False, False], dtype=bool)

In [238]: (dists >= r) & (dists <= r + dr)
Out[238]: 
array([False, False, False, False, False, False, False, False, False,
       False,  True,  True,  True, False, False, False, False, False,
       False, False], dtype=bool)

现在你可以调用np。在组合布尔数组上:

In [239]: np.where((dists >= r) & (dists <= r + dr))
Out[239]: (array([10, 11, 12]),)

In [240]: dists[np.where((dists >= r) & (dists <= r + dr))]
Out[240]: array([ 5. ,  5.5,  6. ])

或者简单地使用花哨的索引用布尔数组索引原始数组

In [241]: dists[(dists >= r) & (dists <= r + dr)]
Out[241]: array([ 5. ,  5.5,  6. ])

公认的答案很好地解释了这个问题。但是,应用多个条件的更Numpythonic方法是使用numpy逻辑函数。在这种情况下,您可以使用np.logical_and:

np.where(np.logical_and(np.greater_equal(dists,r),np.greater_equal(dists,r + dr)))

Try:

np.intersect1d(np.where(dists >= r)[0],np.where(dists <= r + dr)[0])

Try:

import numpy as np
dist = np.array([1,2,3,4,5])
r = 2
dr = 3
np.where(np.logical_and(dist> r, dist<=r+dr))

输出:(数组([2,3,4]),)

您可以查看逻辑函数了解更多详细信息。