Stop processing when the number of consecutive list elements have a unique value

Mostafa Gomaa

I have a long record of data in a list format which I want to separate that record according to a certain condition. I want to get the sum of the list elements so the sum order will stop when consecutive 3 or more elements are equal to 0, then the sum order restarts again from where it stopped.

for ex: part of the list is [8, 2, 1, 1, 2, 0, 0, 0, 0, 0, 6, 0, 2, 0, 0, 0, 6, 0]

the output should be a new list of [14, 8, 6] where: output1: 8 + 2 + 1 + 1 + 2 = 14, output2: 6 + 2 = 8, and output3: 6

I write so far something like below but I have two problems: 1- list index out of range, 2- the fake list extension with three 0 elements at the end of the list

# note: i add three fake 0 at the end of the list to get the correct output
arr = [8, 2, 1, 1, 2, 0, 0, 0, 0, 0, 6, 0, 2, 0, 0, 0, 6, 0, 0, 0, 0]
result = []
cum = 0        
for i in range(0, len(arr)):
   el, el1, el2 = arr[i], arr[i+1], arr[i+2]
   if el != 0:
       cum = cum + el
   if el == 0 and el1 == 0 and el2 == 0: 
       if cum != 0:
          result.append(cum)
          cum = 0
CaptainK

This will solve both your problems:

arr = [8, 2, 1, 1, 2, 0, 0, 0, 0, 0, 6, 0, 2, 0, 0, 0, 6, 0]
    result = []
    cum = 0
    for i in range(0, len(arr) - 2):
        el, el1, el2 = arr[i], arr[i + 1], arr[i + 2]
        if el != 0:
            cum = cum + el
        if el == 0 and el1 == 0 and el2 == 0:
            if cum != 0:
                result.append(cum)
                cum = 0
        elif i == len(arr) - 3:
            cum = el + el1 + el2
            result.append(cum)
            break

    print(result)

The 'elif i == len(arr) - 3:' condition makes sure that your loop breaks when you have reached last 3 elements. Additionally it will execute only when at least 1 element is non-zero from last three.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Count number of consecutive elements in a list

How do I stop the function when I have a unique list?

How to find the list of consecutive elements in a random list where the resultant list should not have greater number than the given number

Finding number of unique elements in the list

Count the total number of subsets that don't have consecutive elements

How to stop tabs when they have empty elements?

How can I add have a property of a list populated with a unique number when I populate the list?

What to use as key when rendering a list of elements in react if the objects have no property which can be used as an unique identifier?

Handle NullReferenceException when processing a list of elements

Python check if indefinite number of consecutive elements in a list satisfy a condition

Finding the average number of consecutive same value elements in matlab

Duplicate elements in a list to have the same length as number of elements in a second list

Consecutive elements in a list

Count consecutive elements in a list

Assign a number to each unique value in a list

Number of consecutive zeroes in a list

Removing elements that have consecutive duplicates

Transform list to list of consecutive elements

How do I stop iterating based on the number of elements on my list?

How to ensure HTML elements have unique ids when repeated in a layout

Find unique elements part of each list amongst unknown number of lists

Count number of unique elements at index in 2D list

Find elements in list that is between elements with that have the same value using a for loop

List with type constraints on consecutive elements

How to find elements in a list is consecutive

Sum consecutive pairs of elements in a list

Constrain a Specman list so it doesn't have identical values in consecutive elements

How to stop when there are no more elements in a list matching your criteria (Python)

Estimating number of consecutive pairs in a list