简体   繁体   English

如何根据相邻元素之间的差异将列表拆分为列表列表?

[英]How can I split a list into a list of lists based on the difference between adjacent elements?

Eg, if I've got 比如,如果我有的话

MAX_ALLOWED_DIFF = 3
nums=[1, 2, 4, 10, 13, 2, 5, 5, 5]

the output should be 输出应该是

groups = [[1, 2, 4], [10, 13], [2, 5, 5, 5]]

The context: I had a pandas.Series object nums and I used 上下文:我有一个pandas.Series object nums我用过

nums = nums.diff().gt(DETECTION_MAX_DIFF_NS).cumsum()).apply(list).tolist()

to subsample in the same fashion but I noticed that there're a lot of duplicates in my Series nums and after I use .unique() method I don't have Series object anymore, I've got numpy.ndarray (1D) instead. 以相同的方式进行子样本但是我注意到我的Series nums有很多重复项,在我使用.unique()方法之后我再也没有Series对象了,我有numpy.ndarray (1D)代替。

I believe I may use sth like pandas.Series(nums.unique) but I don't like this hack. 我相信我可能会像pandas.Series(nums.unique)那样使用但我不喜欢这个hack。

Here's one approach - 这是一种方法 -

>>> import numpy as np
>>> idx = np.r_[0,np.flatnonzero(np.abs(np.diff(nums))>MAX_ALLOWED_DIFF)+1,len(nums)]
>>> [nums[i:j] for (i,j) in zip(idx[:-1],idx[1:])]
[[1, 2, 4], [10, 13], [2, 5, 5, 5]]

So we using drop_duplicates , keep nums stay in pd.Series 所以我们使用drop_duplicates ,保持nums留在pd.Series

nums=nums.drop_duplicates()
nums.groupby(nums.diff().abs().gt(MAX_ALLOWED_DIFF).cumsum()).apply(list).tolist()
Out[447]: [[1, 2, 4], [10, 13], [5]]

Given that you've tagged with numpy too, here's one way to do it: 鉴于你也标记了numpy ,这是一种方法:

thr = 3
ix = np.flatnonzero(np.concatenate([[False], np.abs(np.diff(nums))>thr]))
np.split(nums, ix)

Output 产量

[array([1, 2, 4]), array([10, 13]), array([2, 5, 5, 5])]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM