Home > Mobile >  How to start counter when a condition is met
How to start counter when a condition is met

Time:03-10

If I have a list x=[0.0,0.0,2.0,3.0,0.0,2.0] And I basically want to count the amount of items in the list but only start after the leading zeroes, how would I do that? Keep in mind that I want to count the zero in the middle of the list but not the ones at the beginning. Thanks

Edit: I’ve tried this:

n=0
for i in x:
    while i==0.0:
       break
    if i!=0.0:
       n=n 1

But it didn’t get 4, which is the output I want as I want to include 2.0,3.0,0.0,2.0 only

CodePudding user response:

You can use itertools.dropwhile

from itertools import dropwhile
from collections import Counter

data = [0.0,0.0,2.0,3.0,0.0,2.0]
print(sum(1 for _ in dropwhile(lambda x: x == 0, data))) # 4

print(Counter(dropwhile(lambda x: x == 0, data))) # Counter({2.0: 2, 3.0: 1, 0.0: 1})

CodePudding user response:

You can simply wrap your counter code in a conditional, with something like this:

x=[0.0,0.0,2.0,3.0,0.0,2.0]
start_counting = False
item_count = 0

for i in x:
    if i != 0.0:
        start_counting = True
    if start_counting:
        item_count  = 1

item_count will now have the amount of items in the list without the starting 0.0s

CodePudding user response:

One way to make this an easy one-liner is to turn the list into a string, with the zeroes transformed into whitespace; this allows you to use str.lstrip():

>>> x=[0.0,0.0,2.0,3.0,0.0,2.0]
>>> len(''.join("*" if i else " " for i in x).lstrip())
4

CodePudding user response:

x=[0.0,0.0,2.0,3.0,0.0,2.0]
while x[0] == 0:
    x = x[1:]
print(len(x))

CodePudding user response:

Your added code is conceptually close:

n=0
for i in x:
    ## -----------------
    ## the problem here is you are not incrementing "i"
    ## -----------------
    while i==0.0:
       break
    ## -----------------
    if i!=0.0:
       n=n 1

Let's take your idea an use it to count just the number of leading 0.0 items:

count_leading_zeros = 0
for item in x:
    if item == 0.0:
        count_leading_zeros  = 1
        continue
    break

That will give us 2 and a simple len(x) is 6 and 6 - 2 == 4

Cleaning up the for loop above to do the same as we have done as:

for item in x:
    if item != 0.0:
        break
    count_leading_zeros  = 1

We can create a complete answer as:

x=[0.0,0.0,2.0,3.0,0.0,2.0]
count_all = len(x)
count_leading_zeros = 0
for item in x:
    if item != 0.0:
        break
    count_leading_zeros  = 1
print(count_all - count_leading_zeros)

I feel this is the most straightforward approach, but you have lots of great ones to select from if you feel this is not "pythonic".

CodePudding user response:

I think you can use the 'set()' function to remove duplicate values from your list, then you can be sure that the second element in this set is the first value after the zeroes, this value can be extracted by converting the set to a tuple or a list and using the indices, when you know what the first value is after the zeros, you can find it's position using the 'index()' function and that position is where you need to start counting, so if you subtract this position from the list length you get the list items after the first non-zero value.

x = [0.0, 0.0, 2.0, 3.0, 0.0, 2.0]
print(len(x) - x.index(tuple(set(x))[1])    
  • Related