![](/img/trans.png)
[英]Removing points from list if distance between 2 points is below a certain threshold
[英]How to find consecutive points and their index below.a threshold from a list of points in python?
我有数据点列表,我会查看它们是否高于某个阈值。
我可以计算高于阈值的总点数的百分比,但我需要高于阈值的所有点的索引和点数。 例如
points_above_threshold = [1,1,1,0,0,0,1,1] 1 是,0 是否
我需要一个返回的函数,点的格式为: [line_points,[start_index, end_index]
例如 points_above_threshold 的输出将是 [3,(0,2)],[2,(6,7)]
您的问题缺少有关您正在使用的数据格式的一些细节。 一个好的起点是为您的函数精确指定预期的输入和输出。
例如,如果您的数据是这样的数字列表(浮点数):
[1.56, 2.45, 8.43, ... ]
你的阈值是一个浮点数,你的输出应该是一个元组列表(索引,data_point),如下所示:
[(1, 2.45), (2, 8.43), ... ]
然后你可以编写一个看起来像这样的函数:
def get_points_above_threshold(data_list, threshold):
output = []
for idx, point in enumerate(data_list):
if point > threshold:
output.append((idx, point))
return output
我将尝试回答如何实现您描述的points_above_threshold
函数。 我们可以使用跟踪系统稍微更改上述函数,以计算高于阈值的值的索引范围,如下所示:
def compute_ranges(values, threshold):
start_range = None #
ranges = [] # tuples (start_idx, end_idx), inclusive
for idx, value in enumerate(values): #
if value <= threshold: # This either ends an "active" range, or does nothing if there isn't one.
if start_range is None: # If no current range, continue
continue #
ranges.append((start_range, idx-1)) # Otherwise end current range, append it to ranges, and reset range variables
start_range = None #
else: # Otherwise, we either start an "active" range or continue one that already exists
if start_range is None: #
start_range = idx #
if start_range is not None: # If still an active range, append it (since range could end at end of list)
ranges.append((start_range, #
len(values)-1)) #
final = [(r[1]-r[0]+1, r) for r in ranges] # Do final convert that includes length of range to output
return final
如果我们将此函数应用于具有给定阈值的数字列表,它将以您上面描述的方式输出范围。 例如,如果输入列表是简单的例子
[1,1,1,0,0,0,1,1]
并且阈值是0.5
,那么输出是
[(3, (0, 2)), (2, (6, 7))]
使用enumerate
和pairwise iteration
,我们可以实现您想要的。
# enumerate helps us to isolate the indexes of 1's
points_above_threshold = [1,1,1,0,0,0,1,1]
id_ = [i for i,e in enumerate(a) if e == 1] # list comprehension
print(id_)
[0, 1, 2, 6, 7] # all indexes of 1's
# pairwise iteration helps us find the
# sequences of indexes, e.g. (0,1,2) and (6,7) are sequences
pairwise = [[]]
for item1, item2 in list(zip(id_, id_[1:])):
if item2-item1 == 1:
if not pairwise[-1]:
pairwise[-1].extend((item1,item2))
else:
pairwise[-1].append(item2)
elif pairwise[-1]:
pairwise.append([])
print(pairwise)
[[0, 1, 2], [6, 7]]
# with the code above we've just iterate over the id_ list
# and create another list with the sequences nested
# now using list comprehension we can achieve the output,
# but with tuples nested inside a list
points_above_threshold = [(len(i), (i[0], i[-1])) for i in pairwise]
print(points_above_threshold)
[(3, (0, 2)), (2, (6, 7))]
希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.