Skip to content

Instantly share code, notes, and snippets.

@rodrigodev
Last active April 7, 2017 17:36
Show Gist options
  • Save rodrigodev/8794d49c4a2206e0a94b52d7a92a6cb8 to your computer and use it in GitHub Desktop.
Save rodrigodev/8794d49c4a2206e0a94b52d7a92a6cb8 to your computer and use it in GitHub Desktop.

Revisions

  1. rodrigodev revised this gist Apr 7, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion smart_group_by_nearest.py
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,9 @@ def smart_group_by_nearest(distance, int_list):
    grouped_list[-1].append(x)
    else:
    grouped_list.append([x])
    return [x for x in grouped_list if len(x) > distance]
    return grouped_list
    #remove small groups if wanted
    #return [x for x in grouped_list if len(x) > 1]

    if __name__ == "__main__":
    int_list = [5, 10, 12, 1, 10, 4]
  2. rodrigodev created this gist Apr 6, 2017.
    13 changes: 13 additions & 0 deletions smart_group_by_nearest.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    def smart_group_by_nearest(distance, int_list):
    int_list.sort()
    grouped_list = [[int_list[0]]]
    for x in int_list[1:]:
    if x - grouped_list[-1][0] <= distance:
    grouped_list[-1].append(x)
    else:
    grouped_list.append([x])
    return [x for x in grouped_list if len(x) > distance]

    if __name__ == "__main__":
    int_list = [5, 10, 12, 1, 10, 4]
    print smart_group_by_nearest(1, int_list)