def cluster_by_diff(data, max_gap):
    """
    a function that clusters numbers based on their differences

    based off of a stacktrace answer:
    http://stackoverflow.com/a/14783998
    :param data: any list of floats or ints
    :param max_gap: the largest gap between numbers until starting a new cluster
    :return: nested list
    """
    # since the data needs to be sorted to determine useful differences, sort the data
    data.sort()

    # initialize the nest list
    groups = [[data[0]]]

    # iterate through data
    for x in data[1:]:
        # compare the difference of the first value of the data and the last entry in the groups to the max gap
        if abs(x - groups[-1][-1]) <= max_gap:
            # not larger than gap, append to last group
            groups[-1].append(x)
        else:
            # make new group if larger
            groups.append([x])

    return groups