Arrays 消除不属于单个连接网络的行对

Arrays 消除不属于单个连接网络的行对,arrays,numpy,Arrays,Numpy,给定:一个numpy数组,a,其中每个连续的元素对与行中的其他元素对共享一个元素。 添加了空格以强调元素的成对性 import numpy as np a = np.array([[1,2, 1,3, 1,4, 6,1], [2,3, 2,4, 4,5, 8,5], [6,7, 1,2, 1,5, 2,6], [7,8, 2,3, 8,9, 3,4]]) 共享元素的详细信息很重要:

给定:一个numpy数组,
a
,其中每个连续的元素对与行中的其他元素对共享一个元素。 添加了空格以强调元素的成对性

import numpy as np
 
a = np.array([[1,2,  1,3,  1,4,  6,1],
              [2,3,  2,4,  4,5,  8,5],
              [6,7,  1,2,  1,5,  2,6],
              [7,8,  2,3,  8,9,  3,4]])
共享元素的详细信息很重要:
a[0]
每一对与另一对共享一个元素(即:1)
a[1]
1对与第2对共享,第2对与第3对共享,第3对与第4对共享
a[2]
1对与第4对共享,2对与第3对和第4对共享
a[3]
1对与3对共享,2对与4对共享

问题:我想消除像
a[3]
这样的行,它们的对不构成单个连接的网络。 例如,第一对和第三对
a[3]
,无法“到达”第二对或第四对。
a[3]
中的对形成两个不同的断开网络,因此应消除
a[3]

相比之下,
a[0]
a[1]
a[2]
中的对形成了一个单独的连接网络,因此保留了这些行。(我们可以从任何一对“获取”到任何其他对)


我真的不知道如何处理这个问题。

这是一个有趣的问题。我的做法如下:

import numpy as np

a = np.array([[1, 2, 1, 3, 1, 4, 6, 1],
              [2, 3, 2, 4, 4, 5, 8, 5],
              [6, 7, 1, 2, 1, 5, 2, 6],
              [7, 8, 2, 3, 8, 9, 3, 4],
              [1, 5, 4, 2, 3, 4, 5, 3]])


def is_network(row):
    npairs = row.size/2
    subnets = []

    for value in set(row):
        # find all the pair positions of the unique values in the row
        subnet = set(np.where(row == value)[0] // 2)
        if len(subnet) == npairs:
            # if a single value is present in all pairs, stop here
            return True
        else:
            # collect all the value-specific connections
            subnets.append(subnet)

    # look through all the subnets and try to build a network from
    # which you can access all pairs. since in a network where you can go
    # anywhere from anywhere else, it doesn't matter where you start.
    startnet = subnets[0]
    subnets.remove(startnet)
    i = 0
    while i < len(subnets) and len(startnet) < npairs:
        subnet = subnets[i]
        # whenever you can reach the subnet from the startnet (that is, 
        # when both subnets share at least one pair), add the pairs of the
        # subnet to the startnet. remove the subnet from the list of subnets 
        # because we don't need to loop over it again, and go back to 
        # the beginning of the list, because we might now be able to connect
        # the startnet subnets that we skipped previously. otherwise, continue
        # on with the next subnet.
        if startnet & subnet:
            startnet |= subnet
            subnets.remove(subnet)
            i = 0
        else:
            i += 1

    # if all pairs are included in the startnet, return True, else False
    return len(startnet) == npairs


mask = [is_network(r) for r in a]
print(mask)
a = a[mask]
将numpy导入为np
a=np.数组([[1,2,1,3,1,4,6,1],
[2, 3, 2, 4, 4, 5, 8, 5],
[6, 7, 1, 2, 1, 5, 2, 6],
[7, 8, 2, 3, 8, 9, 3, 4],
[1, 5, 4, 2, 3, 4, 5, 3]])
def is_网络(世界其他地区):
npairs=行大小/2
子网=[]
对于集合(行)中的值:
#查找行中唯一值的所有对位置
subnet=set(np.where(row==value)[0]//2)
如果len(子网)=npairs:
#如果所有对中都存在一个值,请在此处停止
返回真值
其他:
#收集所有特定于值的连接
子网。追加(子网)
#查看所有子网并尝试从中构建网络
#您可以访问所有对。因为在一个你可以去的网络里
#无论你从哪里开始,无论你在哪里。
startnet=子网[0]
子网。删除(startnet)
i=0
而i
到目前为止,您的解决方案似乎运行良好!明天,我将使用10或12列的较大阵列运行一些计时测试,然后返回给您。谢谢你的贡献。太好了,不客气!就编程而言,我不认为有任何错误,我只是不确定某个地方是否存在逻辑谬误。@user109387好的,所以实际上有一个逻辑谬误,上一个版本没有考虑,那就是我添加到矩阵中的最后一行。我的算法现在也解决了这个问题,我很有信心它在任何情况下都能正常工作。代码运行得很好,速度也相当快。谢谢你抓住了逻辑谬误!干得好。