将元素与lis中的任何元素进行比较

2024-10-01 15:48:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试建立一个函数,它可以帮助我通过查找共享相同顶点的区域来获取相邻区域。Region对象有一个方法getVertices,它将返回一个元组列表: [(0,0), (0,1), (1,0), (1,1)]. 它还具有用于设置要进一步重用的Id的属性和方法(setId/getId)。 我试图遍历一个区域列表(变量称为regions),首先分配id,然后再次迭代-对于每个区域中的每个顶点,循环遍历其余的区域,看看是否有任何一个顶点集中包含这个顶点。然而,以下是失败的:

    def findAdjacent(regions):
i = 0
for region in regions:
    region.setId(i)
    i += 1
for region in regions:
    for vertice in region.getVertices():
        for regionN in regions:
            if any(vertice == v for v in regionN.getVertices()):
                region.addAdjacent(regionN.getId)

if any(vertice == v for v in regionn.getVertices()):

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

我的印象是,这可能会失败,因为它认为元组是一个列表,而不是一个明显的情况下,两个元组对象应该比较好。你介意把我的逻辑缺陷指给我看吗


Tags: 对象方法in区域列表foranyregion

热门问题