I would like to group consecutive numbers from a list with a tolerance of 3 between elements of the list. Example : having a list like [242, 243, 244, 246, 2933, 2936, 2937, 2938] I want to have a result like [[242, 243, 244, 246],[2933, 2936, 2937, 2938]]
I am using this code but it gives me errors :
def groupEl(list1, tol):
res = []
last = list1[0]
for ele in list1:
if ele-last <= tol:
yield res
res = []
res.append(ele)
last = ele
yield res
I get on terminal an error message like this : <generator object split_tol at 0x7f359d7f2550> Please help!