Vesper
Vesper

Reputation: 845

How to check continuous negative or positive number inside a list along with a condition in python?

Given data:

list =[-2,-2,-1,-2,-5,-6,-7,-5,-12,-2,-6,-54,-7,-2,3,2,5,68,68,1,5,3,9,7,-1,-1,-4,-4,-6,-7,-8,8]

In this list I want to count the number of continuous 7 -(ve) or +(ve) and along with that I want to apply a condition to see if the numbers are continuous for more than 7 consecutive terms.

  1. Suppose the number of negative term consecutive for more than 7 times, then you will start counting from that 7th -(ve) value, not from the next value.

  2. And suppose if there are 7 consecutive -(ve) terms but after the 7th -(ve) term, there is a +(ve) term then you will start counting from the next term present ahead of 7th -(ve) term).

I wrote a program that can do most of this. Except this part where a condition sees if the consecutive nature is still there or not.

My code:

dev = [-2,-2,-1,-2,-5,-6,-7,-5,-12,-2,-6,-54,-7,-2,3,2,5,68,68,1,5,3,9,7,-1,-1,-4,-4,-6,-7,-8,8]
counts = []
plus_counter = 0
minus_counter = 0
row_counter = 0
answer_counter = 1
for each in dev:

    if each > 0:
        minus_counter = 0
        plus_counter += 1

        if plus_counter == 7:
            count = answer_counter
            row_counter = answer_counter
            counts.append(count)
            plus_counter = 0
            answer_counter += 1

        else:
            counts.append(0)

    elif each < 0:
        plus_counter = 0
        minus_counter += 1

        if minus_counter == 7:
            count = answer_counter
            row_counter = answer_counter
            counts.append(count)
            minus_counter = 0
            answer_counter += 1

        else:
            counts.append(0)

    row_counter += 1
print("list: ", dev)
print("counter: ",counts)

Output:

("list: " -2,-2,-1,-2,-5,-6,-7,-5,-12,-2,-6,-54,-7,-2,3,2,5,68,68,1,5,3,9,7,-1,-1,-4,-4,-6,-7,-8,8)
("counter: " 0,0,0,0,0,0 ,1,0,0,0,0,0,0,2,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,4,0)

Desired output:

("list: " -2,-2,-1,-2,-5,-6,-7,-5,-12,-2,-6,-54,-7,-2,3,2,5,68,68,1,5,3,9,7,-1,-1,-4,-4,-6,-7,-8,8)
("counter: " 0,0,0,0,0,0,1,0,0,0,0,0,"2",0,0,0,0,0,0,"3",0,0,0,0,0,0,0,0,0,0,4,0)

I marked "2" and "3" because this is where I am making the mistake. I want my counter to start from the same 7th number if consecutive nature still continues till next 7 consecutive terms.

This is the actual difference, in my output "2" comes after the 14th consecutive term because I start counting it from 7+1th term but the desired output "2" comes at 13th consecutive term because counting starts from 7th term.

NOTE: "4" is at the right place because counter is reset if the continuity breaks.

Please help, I don't know how to apply this condition within a condition.

Upvotes: 1

Views: 515

Answers (1)

Phoenixo
Phoenixo

Reputation: 2113

When you have plus_counter == 7:, just reinitialize plus_counter = 1 instead of plus_counter = 0. And same for minus_counter.

if plus_counter == 7:
    count = answer_counter
    row_counter = answer_counter
    counts.append(count)
    plus_counter = 1 # HERE IS THE DIFFERENCE
    answer_counter += 1

[...]

if minus_counter == 7:
    count = answer_counter
    row_counter = answer_counter
    counts.append(count)
    minus_counter = 1 # HERE IS THE DIFFERENCE
    answer_counter += 1

Upvotes: 2

Related Questions