Python 如果列表中的连续数在X的范围内,如何得到它的序列
应返回:Python 如果列表中的连续数在X的范围内,如何得到它的序列,python,list,algorithm,data-structures,Python,List,Algorithm,Data Structures,应返回: data = [1, 2, 5, 6, 7, 9, 22, 24, 26, 29] x=2 我的代码 [[1, 2], [5, 6, 7, 9], [22, 24, 26], [29]] 此代码返回1的连续序列 from operator import itemgetter from itertools import groupby for k, g in groupby(enumerate(data), lambda x:x[1]-x[0]): res.appen
data = [1, 2, 5, 6, 7, 9, 22, 24, 26, 29]
x=2
我的代码
[[1, 2], [5, 6, 7, 9], [22, 24, 26], [29]]
此代码返回1的连续序列
from operator import itemgetter
from itertools import groupby
for k, g in groupby(enumerate(data), lambda x:x[1]-x[0]):
res.append(list(map(itemgetter(1), g)))
如何修改此代码以获得上述输出或任何其他方法。非常感谢您的帮助。请使用numpy:
`([[1, 2], [5, 6, 7], [9], [22], [24], [26], [29]] )`
输出:
from numpy import diff, where, split
result= split(data, where(diff(data)>x)[0]+1 )
print(list(map(list, result)))
为此使用numpy:
`([[1, 2], [5, 6, 7], [9], [22], [24], [26], [29]] )`
输出:
from numpy import diff, where, split
result= split(data, where(diff(data)>x)[0]+1 )
print(list(map(list, result)))
如何知道,数组中要添加多少个数字?如何知道,数组中要添加多少个数字?