如果其中的任何列表包含上一行的至少一个值,则过滤一个numpy数组 [英] Filter a numpy array if any list within it contains at least one value of a previous row

查看:374
本文介绍了如果其中的任何列表包含上一行的至少一个值,则过滤一个numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个numpy数组

I have a numpy array

b = np.array([[1,2], [3,4], [1,6], [7,2], [3,9], [7,10]])

现在,我要执行以下操作:我要减少b.我要减少它的方法是查看b的第一个元素,即[1,2],然后基于该元素,删除b中至少包含12的所有元素.在这种情况下,我将消除[1,6][7,2].然后,我将查看[3,4]并消除那些至少包含34的元素.

Now, I want to do the following: I want to reduce b. The way I want to reduce it is that I look at the first element of b , i.e. [1,2] and based on that, I remove all the elements in b that contain at least a 1 or a 2. In this case, I would eliminate [1,6] and [7,2]. Then I would look at [3,4] and eliminate those elements which contain at least 3 or 4.

实际上,我从列表的开头开始,对于每个元素,我删除了包含其中一个元素的其他元素.

In practice, I start from the beginning of the list, and for each element, I delete the other elements containing one of its elements.

我的尝试

for a in b:
    np.insert(b[~np.array([np.any((a==b)[j]) for j in range(len(b))])], 0,a, axis = 0)

可悲的是,这不起作用!

Which sadly doesn't work!

这是我尝试过的方法,但是它不起作用,而且时间太长.还有其他想法吗?

This is what I tried, but it doesn't work and it is too lengthy. Any other ideas?

修改 我认为主要的问题是,当我执行np.any((a==b)[j])时,它仅对那些第一个元素等于a的第一个元素的元素说True,而当它们等于第二个元素时却不说True.

Edit I think the main problem is that when I do np.any((a==b)[j]) it only says True for those elements that have the first element equal to the first element of a, but doesn't say True when they are equal to the second element

编辑2 您认为这行得通吗?

Edit 2 Do you think this will work?

for index, a in enumerate(b):
    np.insert(b[~np.array([np.any(np.logical_or(a[0]==b, a[1]==b)[j]) for j in range(len(b))])], index, a,  axis = 0)

推荐答案

一个简单的解决方案是使用普通的Python循环:

A trivial solution would be to use a normal Python loop:

b = np.array([[1,2], [3,4], [1,6], [7,2], [3,9], [7,10]])

final = []
seen = set()
for row in b.tolist():
    if seen.intersection(row):  # check if one element of the row has already been seen.
        continue
    else:
        # No item has been seen, so append this row and add the contents to the seen set.
        seen.update(row)
        final.append(row)

print(final)
# [[1, 2], [3, 4], [7, 10]]

我不确定这种问题是否有很好的NumPy函数,但是使用纯Python应该已经非常快了.

I'm not sure if there's a good NumPy function for this kind of problem but it should be pretty fast with pure Python already.

这篇关于如果其中的任何列表包含上一行的至少一个值,则过滤一个numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆