Skip to content

Instantly share code, notes, and snippets.

@skatenerd
Last active August 29, 2015 14:06
Show Gist options
  • Save skatenerd/b124d872e49be62649df to your computer and use it in GitHub Desktop.
Save skatenerd/b124d872e49be62649df to your computer and use it in GitHub Desktop.
python clump
from random import shuffle
to_clump = [0, 0.2, 5, 9, 9.1, 100, 100.2]
shuffle(to_clump)
def clump_same(comparables, distance_threshold):
def do_clump(so_far, current):
last_clump = so_far[-1]
lowest_in_last_clump = last_clump[0]
if (current - lowest_in_last_clump) < distance_threshold:
last_clump.append(current)
else:
so_far.append([current])
return so_far
sorted_list = sorted(to_clump)
return reduce(do_clump, sorted_list[1:], [[sorted_list[0]]])
def skim_clumps(to_clump, threshold):
return [clump[-1] for clump in clump_same(to_clump, threshold)]
print clump_same(to_clump, 0.5)
print skim_clumps(to_clump, 0.5)
#[[0, 0.2], [5], [9, 9.1], [100, 100.2]]
#[0.2, 5, 9.1, 100.2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment