I have a list of x
,y
coordinates that I need to sort based on the x
coordinate, then y
coordinate when x
is the same and eliminate duplicates of the same coordinates. For example, if the list is:
[[450.0, 486.6], [500.0, 400.0], [450.0, 313.3], [350.0, 313.3], [300.0, 400.0],
[349.9, 486.6], [450.0, 313.3]]
I would need to rearrange it to:
[[300.0, 400.0], [349.9, 486.6], [350.0, 313.3], [450.0, 313.3], [450.0, 486.6],
[500.0, 400.0]]
(with one duplicate of [450.0, 313.3]
removed)
That is the normal sort order for a list of lists, anyway. De-dupe it with a dict.
>>> L = [[450.0, 486.6], [500.0, 400.0], [450.0, 313.3], [350.0, 313.3], [300.0, 400.0], [349.9, 486.6], [450.0, 313.3]]
>>> sorted({tuple(x): x for x in L}.values())
[[300.0, 400.0],
[349.9, 486.6],
[350.0, 313.3],
[450.0, 313.3],
[450.0, 486.6],
[500.0, 400.0]]
As we are sorting anyway we can dedupe with groupby
:
>>> import itertools
>>> [k for k, g in itertools.groupby(sorted(data))]
[[300.0, 400.0], [349.9, 486.6], [350.0, 313.3], [450.0, 313.3], [450.0, 486.6], [500.0, 400.0]]
A few timings:
>>> import numpy as np # just to create a large example
>>> a = np.random.randint(0, 215, (10000, 2)).tolist()
>>> len([k for k, g in groupby(sorted(a))])
8977 # ~ 10% duplicates
>>>
>>> timeit("[k for k, g in groupby(sorted(a))]", globals=globals(), number=1000)
6.1627248489967315
>>> timeit("sorted({tuple(x): x for x in a}.values())", globals=globals(), number=1000)
6.654527607999626
>>> timeit("sorted(unique(a, key=tuple))", globals=globals(), number=1000)
7.198703720991034
>>> timeit("np.unique(a, axis=0).tolist()", globals=globals(), number=1000)
8.848866895001265
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With