栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

脾气暴躁的地方

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

脾气暴躁的地方

您的特定情况下 ,最好的方法 将两个条件更改为一个条件:

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

它仅创建一个布尔数组,在我看来是更易于阅读,因为它说,

dist
内部的
dr
还是
r
(尽管我将重新定义
r
为您感兴趣的区域的中心,而不是开始的位置,所以
r = r +dr/2.
)但这并不能回答您的问题。


问题的答案: 如果您只是想过滤出不符合标准的元素,则
实际上并不需要:

where``dists

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

因为

&
将会为您提供基本元素
and
(必须带括号)。

或者,如果您

where
出于某些原因要使用,可以执行以下操作:

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

原因:
不起作用的原因是因为

np.where
返回的是索引列表,而不是布尔数组。您试图
and
在两个数字列表之间移动,这些数字当然没有您期望的
True
/
False
值。如果
a
b
都是两个
True
值,则
aand b
返回
b
。所以说些什么
[0,1,2] and [2,3,4]
只会给你
[2,3,4]
。它在起作用:

In [230]: dists = np.arange(0,10,.5)In [231]: r = 5In [232]: dr = 1In [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 >= rOut[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 + drOut[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.where
组合的布尔数组:

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. ])


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/633894.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号