'How to find consecutive items in a list, a set of 5, setting counter = 0 and reset the counter to check for next 5 consecutive values?
I have a list in which there are consecutive and non-consecutive values as shown in the code. I want to find 5 consecutive values and then append the 4 values excluding the first one in new list row1. for eg.201,202,203,204,205 this is a set of 5 consecutive values and I want to store 202,203,204,205 in row1 list. Then the new set begins from 206 onwards. If there is a set of 5 then the same approach like above should be followed. I have done the simple coding for it. But the output which I get when I print row1 is-- [202, 203, 204, 205, 206, 231]. I WANT [202, 203, 204, 205, 231]. What "if" condition should I provide to get the desired result? I had used break---if count becomes 4, but it stops the execution of the loop. So I used continue but it doesn't give the required output. Any help is much appreciated.
row1 = []
l = [201, 202, 203, 204, 205, 206, 230, 231]
count = 0
for i in range(len(l) - 1):
if l[i+1] == l[i] + 1:
row1.append(l[i+1])
count += 1
if count == 4:
continue
else:
count = 0
print(row1)
Thanks for all the replies but it did not resolve my above query. I am adding more information here for my intended output. The set can be maximum of 5 consecutive values. Even if they are 2 immediate consecutive values, or 3 or 4 still it will be a set but maximum should be 5. The values will be in the set if the difference is of 1. For example if I have 206, 210 then that is not the set. Only if in my list I get immediate values like 206, 207, 208 even if they are a set of 3, the output will have 207, 208. I have shown examples below on how the output should be. I am trying to write a code which works for all these cases. I really appreciate the help. Sorry if anything is not clear.
Examples-----
1) li = [11, 12, 224] output should be [12]
2) li = [11, 12, 13, 14, 15, 224] output should be [12, 13, 14, 15]
3) li = [11, 12, 13, 14, 15, 16, 224] output should be [12, 13, 14, 15]
4) li = [11, 12, 13, 14, 15, 224, 225] output should be [12, 13, 14, 15, 225]
5) li = [184, 185, 186, 187, 201, 202, 203, 204, 205, 206] output should be [185, 186, 187, 202, 203, 204, 205]
6) li = [201, 202, 203, 204, 205, 206, 230, 231] output should be [202, 203, 204, 205, 231]
Solution 1:[1]
This code does exactly what OP wants. I begin at index 0 and append the next four consecutive numbers(if there are that much). After that I move to the element after the last element in the previous consecutive and start all over again.
row1 = []
l = [201, 202, 203, 204, 205, 206, 230, 231]
# Counter to iterate through l
i = 0
n = len(l)
while i < n:
for j in range(1, 5):
ind = i + j
# We use try and except so that we can handle any out of bounds errors
try:
curr = l[ind]
prev = l[ind-1]
if curr == prev+1:
row1.append(curr)
else:
i = ind
break
except:
i = ind
break
# After reaching the last consecutive in the five, move on to the next element
if j == 4:
i = ind + 1
Output for this:
[202, 203, 204, 205, 231]
Solution 2:[2]
You can use sum of 1st and 5th element and sum of range(1st ele, 5th ele + 1) if the sum is equals that means numbers are consecutive
lst = [201, 202, 203, 204, 205, 206, 230, 233, 234, 235, 236, 237, 239]
i, res = 0, []
while (i + 4) < len(lst):
sub = lst[i: i + 5]
if sum(range(sub[0], sub[-1] + 1)) == sum(sub):
res += lst[i + 1:i + 5]
i += 5
else:
i += 1
print(res) # [202, 203, 204, 205, 234, 235, 236, 237]
Solution 3:[3]
following my proposal if i got your point right :
from math import ceil
l = [201, 202, 203, 204, 205, 206, 230, 231, 234, 235, 236, 237, 238]
l = sorted(l)
print([l[i+1:i+5] for i in range(0,max(len(l), int(ceil(len(l)/5.0)*5)),5) if l[i+1:i+5] !=[]])
Result:
[[202, 203, 204, 205], [230, 231, 234, 235], [237, 238]]
Solution 4:[4]
Not completely sure, but it appears to me that you want to find all sequences of 5 elements in a row. If that is the case you need a list of lists as a result. The counter can then be replaced by the length of the list holding the current sequence:
l = [201, 202, 203, 204, 205, 206, 230, 231, 234, 235, 236, 237, 238]
sequences = []
count = 0
current_sequence = []
for i in range(len(l) - 1):
if l[i+1] == l[i] + 1:
current_sequence.append(l[i+1])
if len(current_sequence) == 4:
sequences.append(current_sequence)
current_sequence = []
continue
else:
current_sequence = []
print(sequences)
Output for input with two sequences:
[[202, 203, 204, 205], [235, 236, 237, 238]]
A better solution would be this I think as you do not create lists that do not appear in the result:
l = [201, 202, 203, 204, 205, 206, 207, 230, 231, 234, 235, 236, 237, 238]
sequences = []
i=0
while i < len(l) - 4:
if l[i] == l[i+1]-1 == l[i+2]-2 == l[i+3]-3 == l[i+4]-4:
sequences.append(l[i+1:i+5])
i=i+5
else:
i=i+1
print(sequences)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Emmanuel |
| Solution 2 | deadshot |
| Solution 3 | baskettaz |
| Solution 4 |
