Created
September 22, 2018 09:15
-
-
Save duchenpaul/50b771c21deef5126c6ce238f6830617 to your computer and use it in GitHub Desktop.
Grouping data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cluster(data, maxgap): | |
'''Arrange data into groups where successive elements | |
differ by no more than *maxgap* | |
>>> cluster([1, 6, 9, 100, 102, 105, 109, 134, 139], maxgap=10) | |
[[1, 6, 9], [100, 102, 105, 109], [134, 139]] | |
>>> cluster([1, 6, 9, 99, 100, 102, 105, 134, 139, 141], maxgap=10) | |
[[1, 6, 9], [99, 100, 102, 105], [134, 139, 141]] | |
''' | |
data.sort() | |
groups = [[data[0]]] | |
for x in data[1:]: | |
if abs(x - groups[-1][-1]) <= maxgap: | |
groups[-1].append(x) | |
else: | |
groups.append([x]) | |
return groups | |
if __name__ == '__main__': | |
import doctest | |
print(doctest.testmod()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment