root
root

Reputation: 80436

Python: Uniquefying a list with a twist

Lets say I have a list:

L = [15,16,57,59,14]

The list contains mesurements, that are not very accurate: that is the real value of an element is +-2 of the recorded value. So 14,15 and 16 can have the same value. What I want to do is to uniquefy that list, taking into account the mesurement errors. The output should therefor be:

l_out = [15,57]

or

l_out = [(14,15,16),(57,59)]

I have no problem producing either result with a for loop. However, I am curious if there could be a more elegant solution. Ideas much appriciated.

Upvotes: 5

Views: 341

Answers (4)

ecatmur
ecatmur

Reputation: 157484

Here's how I'd do this in a pure-Python approach:

s = sorted(L)
b = [i + 1 for i, (x, y) in enumerate(zip(s, s[1:])) if y > x + 2]
result = [s[i:j] for i, j in zip([None] + b, b + [None])]

Here b is the list of "breaks", indices where a cluster ends.

Upvotes: 2

Ryan Roemer
Ryan Roemer

Reputation: 997

If you want standard lib python, itertool's groupby is your friend:

from itertools import groupby

L = [15,16,57,59,14]

# Stash state outside key function. (a little hacky).
# Better way would be to create stateful class with a __call__ key fn.
state = {'group': 0, 'prev': None}
thresh = 2

def _group(cur):
    """Group if within threshold."""
    if state["prev"] is not None and abs(state["prev"] - cur) > thresh:
        state["group"] += 1 # Advance group
    state["prev"] = cur
    return state["group"]

# Group, then drop the group key and inflate the final tuples.
l_out = [tuple(g) for _, g in groupby(sorted(L), key=_group)]

print l_out
# -> [(14, 15, 16), (57, 59)]

Upvotes: 2

root
root

Reputation: 80436

As lazyr pointed out in the comments, a similar problem has been posted here. Using the cluster module the solution to my problem would be:

>>> from cluster import *
>>> L = [15,16,57,59,14]
>>> cl = HierarchicalClustering(L, lambda x,y: abs(x-y))
>>> cl.getlevel(2)
[[14, 15, 16], [57, 59]]

or (to get unique list with mean values of each group):

>>> [mean(cluster) for cluster in cl.getlevel(2)]
[15, 58]

Upvotes: 5

unddoch
unddoch

Reputation: 6014

For loop is the simplest way, but if you really want a single-line code:
l_out = list(set(tuple([tuple(filter(lambda i: abs(item - i) < 3, L)) for item in L])))
Very unclear though, I would prefer the for version :)

Upvotes: -1

Related Questions