布尔和和numpy查找匹配对

时间:2016-01-06 21:05:49

标签: python arrays numpy

我试图在numpy数组中找到镜像。特别是,{ code: 2000, message: "create success" } // or { code: 2001 message: "user already exists" } 但我想排除具有相同值(x,y) == (y,x)的元组。

给定一个包含浮点数的大小为(x,x)的numpy数组pckList

我有以下代码:

(198L,3L)

返回给定的数字,比方说73

np.sum([x==pckLst[:,2] for x in pckLst[:,1]])

返回一个更大的数字,比方说266。

有人可以解释一下这是怎么发生的吗?

我认为第一行返回True,当被视为元组np.sum([x==pckLst[:,2] for x in pckLst[:,1]] and [x==pckLst[:,1] for x in pckLst[:,1]]) 时,第二行仅在(x,y) == (any,y)时返回true。

这是对的吗?

编辑: 进一步解释:

(x,y) == (y,x)

现在我想找pckLst=[[ 112.066, 6.946, 6.938], [ 111.979, 6.882, 7.634], [ 112.014, 6.879, 7.587], [ 112.005, 6.887, 7.554], [ 111.995, 6.88, 6.88 ], [ 112.048, 6.774, 6.88 ], [ 111.808, 7.791, 7.566], [ 111.802, 6.88, 6.774]],因为[ 112.048, 6.774, 6.88 ]。但是,(6.88, 6.774) == (6.774, 6.88)不应视为匹配。

2 个答案:

答案 0 :(得分:1)

不是在这里评论您的代码,而是更简单的实现

div

答案 1 :(得分:0)

"和"的论据在你的例子中是python-lists。如果列表不为空,则列表的真值为True。这就是为什么你在后一种情况下获得更大的金额。 这将返回带有(x,y)==(y,x)的元素之和。它显然只有你对这个总和而不是特定的指数感兴趣才有效:

import numpy
pckLst = numpy.array([[ 112.066,    6.946,    6.938],
       [ 111.979,    6.882,    7.634],
       [ 112.014,    6.879,    7.587],
       [ 112.005,    6.887,    7.554],
       [ 111.995,    6.88,    6.88 ],
       [ 112.048,    6.774,    6.88 ],
       [ 111.808,    7.791,    7.566],
       [ 111.802,    6.88,    6.774]])

coords = pckLst[:,1:]
equal_ids = numpy.ravel(coords[:,:1] != coords[:,1:])
unequal_coords = coords[equal_ids]
flipped = numpy.fliplr(unequal_coords)
coords_tuple_set = set(tuple(map(tuple, unequal_coords)))
flipped_tuple_set = set(tuple(map(tuple, flipped)))

print coords_tuple_set
print flipped_tuple_set
# need to devide by two, because we get (x,y) and (y,x) by the intersection
print "number of mirrored points:", 
print len(coords_tuple_set.intersection(flipped_tuple_set))/2