通过整数的连续性来分隔列表

时间:2013-02-16 07:24:24

标签: python list math tuples signal-processing

我想知道是否有人可以帮我找到一个好的方法来获得与某些xy数据集中的峰相关联的x索引。 我知道d2y / dx2< 0和dy / dx = 0表示x处的峰值中心。拐点为d2y / dx2 = 0且| dy / dx |> 0。但是我希望将数据从变形到变形作为列表。 (所以列表是因为我需要得到几个峰值)

所以,我也能够为拐点到拐点值列出这个:

inflection_indices = [x for x in list( np.where( dY2dX2 < 0.0 )[0] )]

但是,这只是给我一个列出所有峰值的列表。

我想有一个列表列表,其中整个列表的每个元素都是一个单独的峰值。

像这样:[14,15,16,17,18,19,20,89,90,91,92,93,94,204,205,206,207]

我想要的时候:[[14,15,16,17,18,19,20],[89,90,91,92,93,94],[204,205,206,207]]

感谢您提供任何帮助,我真的很感激。

编辑:我完全删除了所有元组,所以我可以在以后添加它们,我认为这个问题现在更多地是问题的核心。

1 个答案:

答案 0 :(得分:0)

我想我找到了答案,但它根本不是很优雅......

self.list_of_peak_indeces = [x for x in list(np.where(self.dY2dX2 < 0.0)[0])]
temp_list = []
self.main_lists_of_peak_indices = []
difference_between_indices = np.hstack([np.diff(self.list_of_peak_indeces), np.diff(self.list_of_peak_indeces)[-1]+1])
    for index, item in enumerate(self.list_of_peak_indeces):
        if(np.absolute(difference_between_indices[index])!=1):
            if(temp_list!=[]):
                temp_list.append(item)
                self.main_lists_of_peak_indices.append(temp_list)
            temp_list = []
        else:
            temp_list.append(item)
相关问题