简体   繁体   English

找到几个相同的两个数组的元组成员?

[英]Finding almost equal tuple members of two arrays?

I have 2 lists of positional tuples (x,y data). 我有2个位置元组列表(x,y数据)。 I would like to return 2 arrays or lists of the indexes for positions (or a tuple) that are in both lists. 我想为两个列表中的位置(或元组)返回2个数组或索引列表。 However the positional data values will not be exactly equal, there will be an uncertianty of +/- 4 on both the x and y coordinates. 然而,位置数据值将不完全相等,在x和y坐标上将存在+/- 4的不确定性。

For example: 例如:

A=[(1168.593,9.874), (1799.244,40.201),(780.533,12.636)]
B=[(1170.909,8.194), (793.149,10.885), (1801.493,41.603)]

it should return: 它应该返回:

c=[(0,0),(1,2)]

or: 要么:

d=[0,1] #indexes for A
e=[0,2] #indexes for B

Either one would be fine to use. 任何一个都可以使用。

Is there a function in Python you can use that returns the indexes of matching data in 2 lists, by specifying a +/- value as well? 您是否可以使用Python中的函数返回2个列表中匹配数据的索引,同时指定+/-值?

I need to do this for 3 lists of ~400 tuples each, which are not equal in size. 我需要为每个约400个元组的3个列表执行此操作,这些列表的大小不相等。

I was thinking of even using something like: 我想甚至使用类似的东西:

common=[a in A for a in B]

and somehow specifying a range for a , just looking at the x and y data and return the indices instead of true/false, but I really don't know how to approach this. 并以某种方式指定的范围a ,只是看X和Y数据并返回,而不是真/假的指标,但我真的不知道如何处理这一点。 Is a loop the only way to do this, by looking at each value separately, getting a difference between them and seeing if this is < 4, then getting the indexes? 循环是唯一的方法,通过分别查看每个值,获得它们之间的差异,看看它是否<4,然后获取索引?

也许你可以考虑使用math模块中的函数isclose

How about a brute-force solution? 蛮力解决方案怎么样?

In [5]: c = []

In [6]: for i, (x1, y1) in enumerate(A):
   ...:     for j, (x2, y2) in enumerate(B):
   ...:         if (x1 - 4 <= x2 <= x1 + 4) and (y1 - 4 <= y2 <= y1 + 4):
   ...:             c.append((i,j))
   ...:
   ...:

In [7]: c
Out[7]: [(0, 0), (1, 2)]

Of course, you can replace the conditional with whatever you want. 当然,你可以用任何你想要的条件替换条件。 Likely, using math.isclose is a good idea. 可能,使用math.isclose是个好主意。 There may be a better numpy way of doing this in a vectorized fashion. 以矢量化的方式可能有更好的numpy方式。 But this should work if efficiency isn't a concern. 但如果效率不是问题,这应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM