通过阈值将python列表分成多个列表?

时间:2019-12-06 17:43:24

标签: python algorithm

比方说,我有一个值列表,其中值被分组在一起。
例如

lst = [5.0, 5.2, 4.0, 4.1, 4.05, 5.1]

如何将其分为两组,所以输出为[5.0, 5.2, 5.1], [4.0, 4.05, 4.1]?我确定每个组中的每个值都在其合作伙伴的0.2之内。为该间隔指定阈值的最佳方法是什么?

这是到目前为止我尝试过的。

unique_values = []
    for x in range(len(lst)):
        isInList=False
        for y in range(len(unique_values)):
            if compare_threshold(lst[x], unique_values[y]): #returns true if the two values are within the threshold
                isInList=True
        if isInList == False:
            unique_values.append(lst[x])
    print(unique_values)

这为我提供了列表中单个唯一点的列表。

[55.02123905, 55.02167612, ... 137.0536191, 137.0536604] (118 values)

输出:

[55.02123905, 57.03325912, 67.0292289, 69.0339192, 71.01344708, 92.02611874, 94.04178177, 109.054014, 112.0537901, 119.0396714, 120.0424827, 136.0683814, 137.0527245]

2 个答案:

答案 0 :(得分:1)

组的最大大小为0.2。 需要_entier来解决浮点精度问题。

In [183]: def grouper(x, delta=0.2, _entier=1e-6):
     ...:     out, part = [], []
     ...:     for item in sorted(x):
     ...:         if not part:
     ...:             part = [item]
     ...:         elif item - part[0] <= delta + _entier:
     ...:             part.append(item)
     ...:         else:
     ...:             out.append(part)
     ...:             part = [item]
     ...:     if part:
     ...:         out.append(part)
     ...:     return out

In [184]: grouper(lst)
Out[184]: [[4.0, 4.05, 4.1], [5.0, 5.1, 5.2]]

答案 1 :(得分:0)

尝试一下:

def grouper(list_, threshold):
    list_.sort()
    prev = None
    group = []
    for item in list_:
        if not prev or item - prev <= threshold:
            group.append(item)
        else:
            yield group
            group = [item]
        prev = item
    if group:
        yield group

lst = [5.0, 5.2, 4.0, 4.1, 4.05, 5.1]
output = list(grouper(lst, threshold=0.2))

输出

[[4.0, 4.05, 4.1], [5.0, 5.1, 5.2]]