mardi 15 décembre 2015

If statement in list comprehension with lambda

I have

listName = [[0,1,2,15,16,17,2,3,4,6,8,9]]

My line of code

[list(g) for k, g in groupby(listName, key=lambda i,j=count(): i-next(j))]

is splitting listName into [[0,1,2],[15,16,17],[2,3,4],[6,8,9]] I want the split to happen only if the next number is less than the preceding number. i.e I want my listName to split into

[[0,1,2,15,16,17],[2,3,4,6,8,9]]

Thanks! :)

Aucun commentaire:

Enregistrer un commentaire