0

I am trying to split a list of unknown length into multiple lists based on the difference between elements in the list in python. The list contains consecutive integers until a "break" occurs, and another set of consecutive integers begins. I have been trying to get the syntax right with a very basic example, but have had some trouble:

a = [1,2,3,4,5,9,10,11,12,13] 

Ideally I'd like to break this into two lists:

b = [1,2,3,4,5] and c = [9,10,11,12,13] based on the difference between elements 5 and 9 in the list.

I have tried using different forms of list comprehension, but haven't gotten the commands quite right.

b = [e for e in a if e+1 - e == 1]

print b

b = [1,2,3,4,5,9,10,11,12,13] 

When I thought it would return b=[1,2,3,4,5]

Any insight would be great. I have been searching around and just have not found a way to reliably split the list by the occurrence and an unknown break in the sequence of numbers. I have considered manipulating the range() function in conjunction with remove() or del() to get ride of the parts that I do not need in the list, but haven't found a workable way to do it using those functions either. I am a novice programmer that works mainly in the arcpy module for arcgis, and have limited actual programming experience outside of working in arcgis.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
sai
  • 9
  • 3

0 Answers0