[英]What is a pythonic way to find set of lists which have all common elements?
Say I have a list a=[[1,2],[1.00000001,2.000000001],[2,3],[4,5],[4.0000000002,5.0000006]]
, and from that I want to get only a=[[1,2],[2,3],[4,5]]
because the other lists have all elements close to some other element of the list.假设我有一个列表
a=[[1,2],[1.00000001,2.000000001],[2,3],[4,5],[4.0000000002,5.0000006]]
,我只想得到a=[[1,2],[2,3],[4,5]]
因为其他列表的所有元素都靠近列表中的某个其他元素。 How do we do this in Python?我们如何在 Python 中做到这一点? Can
set
be used in some way?可以以某种方式使用
set
吗?
I tried using numpy's allclose
function but then we have to compare all n(n-1)/2 pairs in the list a, which is clearly inefficient.我尝试使用 numpy 的
allclose
function 但是我们必须比较列表 a 中的所有 n(n-1)/2 对,这显然是低效的。 Is there a more efficient Pythonic way to do this?有没有更有效的 Pythonic 方式来做到这一点?
Edit: I just used some integers in the example, the inputs are floats in general.编辑:我只是在示例中使用了一些整数,输入通常是浮点数。
If the values are like that (ie what looks like floating point inaccuracy, you can round the values to the precision you're interested in, then use set()
to reduce things to unique tuples:如果值是这样的(即看起来像浮点不准确,您可以将值四舍五入到您感兴趣的精度,然后使用
set()
将事物减少为唯一元组:
>>> a=[[1,2],[1.00000001,2.000000001],[2,3],[4,5],[4.0000000002,5.0000006]]
>>> a_r = [tuple(round(v, 2) for v in pair) for pair in a]
[(1, 2), (1.0, 2.0), (2, 3), (4, 5), (4.0, 5.0)]
>>> set(a_r)
{(2, 3), (4, 5), (1, 2)}
For the data sample in your list and the expected result, as you can use Numpy, i'd suggest to use dtype
to convert to integer and use numpy.unique .对于列表中的数据样本和预期结果,因为您可以使用 Numpy,我建议使用
dtype
转换为 integer 并使用numpy 。
So, given your list:所以,给定你的清单:
a=[[1,2],[1.00000001,2.000000001],[2,3],[4,5],[4.0000000002,5.0000006]]
You can do as follows:您可以执行以下操作:
import numpy as np
a_np = np.unique(np.array(a, dtype=np.uint), axis=0)
print(a_np)
# [[1 2]
# [2 3]
# [4 5]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.