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. How do we do this in Python? Can set
be used in some way?
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. Is there a more efficient Pythonic way to do this?
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:
>>> 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 .
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]]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.