I am going to write a function for this for a single list, for example
>>> compact([6.23234121,6.23246575], tol=.01)
[6.23234121]
Then you can only make it work with your nested structure with [compact(l) for l in lst].
, ; @DSM [0, 0.005, 0.01, 0.015, 0.02] [0, 0.0.15] (, > >=, [0, 0.01, 0.02]). - , , .
-, , . O (n ^ 2):
def compact(lst, tol):
new = []
for el in lst:
if all(abs(el - x) > tol for x in new):
new.append(el)
return compact
. , , . - :
import collections
import math
def compact(lst, tol):
round_digits = -math.log10(tol) - 1
seen = collections.defaultdict(set)
new = []
for el in lst:
rounded = round(seen, round_digits)
if all(abs(el - x) > tol for x in seen[rounded]):
seen[rounded].add(el)
new.append(el)
return new
tol 0.01, round_digits 1. , 6.23234121 seen 6.2. 6.23246575, 6.2 , , , tol , . , , , .
O (n k), k - , . , k < n ( , , tol). , , , , , .
- ; , .