#Find values that are in range
in_range = [lo_lim <= v <= hi_lim for v in values]
#Find runs of in-range values
runs = [sum(1 for _ in group) for v, group in groupby(in_range) if v]
#Estimate total time spent in-range
total_time = sum(v if v > 1 else (Buffer_Value*sample_rate) for v in runs)
我正在尝试增加此代码以获取两组值,以及2对hi / lo限制, 计算在这些限制范围内花费的总时间,以及在限制情况下的组合限制&#39;在同一点上也是如此,即
如果有100个数据点(两个数据集的长度相同,请检查每个点,
if values_1[45] and values_2[45] are in their respective limits
然后按范围计算。 基本上将此if转换为生成器表达式:
if lo_lim_1<=Data_Points_1[i]<=hi_lim_1 and lo_lim_2<=Data_Points_2[i]<=hi_lim_2:
计算运行次数,如果运行长度是一个数据点,则应用缓冲区,否则应用采样率转换。
答案 0 :(得分:2)
如果我理解你的问题,这应该有效。基本思路是将zip
两个序列组合成对应的值对,然后使用and
操作查找它们都在相应范围内的情况:
#Find values that are in range
in_range = [lo_lim1 <= v1 <= hi_lim1 and lo_lim2 <= v2 <= hi_lim2 for v1, v2 in zip(values1, values2)]
# code is unchanged from here
#Find runs of in-range values
runs = [sum(1 for _ in group) for v, group in groupby(in_range) if v] # this is the same as yours
#Estimate total time spent in-range
total_time = sum(v if v > 1 else (Buffer_Value*sample_rate) for v in runs)
在你的情况下,如果你使用python 2.x,你可以使用itertools.izip
而不是zip
来节省一些内存,而对于python 2.x和3.x你可以使用生成器表达式以节省更多:
#Find values that are in range
in_range = (lo_lim1 <= v1 <= hi_lim1 and lo_lim2 <= v2 <= hi_lim2 for v1, v2 in zip(values1, values2))
#Find runs of in-range values
runs = (sum(1 for _ in group) for v, group in groupby(in_range) if v) # this is the same as yours
#Estimate total time spent in-range
defval = Buffer_Value*sample_rate
total_time = sum(v if v > 1 else defval for v in runs)