1I'm looping over a series of numbers looking for range of numbers (e.g. <100).
E.g.: list = [1, 2, 3, 125, 7, 8, 9, 200]. I want to get in a file: 1-3, 7-9. The problem I faced is that the outer loop will reiterate over items in an inner loop, so the output I get is: 1-3, 2-3, 3, 7-9, 8-9, 9.
My current strategy which works:
counter = 1
for i in range(len(list)): # outer loop
if counter > 1: # prevents the outer loop from iterating the numbers iterated in inner loop
counter -= 1
continue
elif counter <=1:
while list[i] < 100:
i +=1
counter +=1
if list[i] > 100:
print list[i-counter], '-', list[i]
break
I'm wondering if there is a more pythonic way to get the outer loop to skip over items that have been iterated in the inner loop, instead of using an additional counter (like I did above). Thanks.
Edit: There have been few replies that focused on consecutive numbers. My mistake, the number don't have to be consecutive. I just need the first and last number in that range E.g. list = [1,4,8, 12, 57, 200, 4,34, 300]. Output: 1 - 57, 4 - 34. The list and criteria is dependent on user. The criteria will always be a number with comparison operator '<'. Thanks.