Home > Blockchain >  How to find consecutive items in a list, a set of 5, setting counter = 0 and reset the counter to ch
How to find consecutive items in a list, a set of 5, setting counter = 0 and reset the counter to ch

Time:03-10

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)

CodePudding user response:

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]

CodePudding user response:

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]]

CodePudding user response:

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)

CodePudding user response:

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]
  • Related