加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python数组过滤越界

发布时间:2020-12-20 13:13:21 所属栏目:Python 来源:网络整理
导读:我尝试在 python中执行此操作,但是我收到一个错误: import numpy as nparray_to_filter = np.array([1,2,3,4,5])equal_array = np.array([1,5,5])array_to_filter[equal_array] 这导致: IndexError: index 5 is out of bounds for axis 0 with size 5 是什
我尝试在 python中执行此操作,但是我收到一个错误:

import numpy as np
array_to_filter = np.array([1,2,3,4,5])
equal_array = np.array([1,5,5])
array_to_filter[equal_array]

这导致:

IndexError: index 5 is out of bounds for axis 0 with size 5

是什么赋予了?我以为我在这里做了正确的操作.

如果我这样做,我期待着

array_to_filter[equal_array]

它会回来

np.array([1,5])

如果我不在正确的轨道上,我怎么能这样做呢?

解决方法

在最后一个语句中,数组的索引是1,5和5.索引5指的是数组中的第6个元素,而您只有5个元素. array_to_filter [5]不存在.

[i for i in np.unique(equal_array)if if in array_to_filter]

会回复你想要的答案.如果它也存在于array_to_filter中,则返回equal_array中的每个唯一值

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读