Example:
sample_list = [1,2,4,7,9,10,15,20]
# we have some criteria where consecutive numbers >= 3 apart forms a sublist:
result_list = [[1,2,4],[7,9,10],[15],[20]]
in python, whats an efficient way to do this?
CodePudding user response:
Define:
def batches(xs, criteria):
ys = []
for x in xs:
if len(ys) != 0 and criteria(ys[-1], x):
yield ys
ys = []
ys.append(x)
if len(ys) != 0:
yield ys
Usage:
>>> xs = [1, 2, 4, 7, 9, 10, 15, 20]
>>> list(batches(xs, lambda a, b: a 3 <= b))
[[1, 2, 4], [7, 9, 10], [15], [20]]
CodePudding user response:
zip()
is your friend for this type of thing. It can produce pairs of list items combined with their predecessor. From that you can determine the indexes of "break" elements that are at or beyond the specified distance from their predecessor.
From these indexes you can use zip again to obtain start/end range values that you can use to subscript the list.
L = [1,2,4,7,9,10,15,20]
distance = 3
breaks = [i for i,(a,b) in enumerate(zip(L,L[1:]),1) if b-a>=distance]
result = [L[s:e] for s,e in zip([0] breaks,breaks [None])]
print(result)
[[1, 2, 4], [7, 9, 10], [15], [20]]