0

The integer list is essentially a random integer list of mostly integers in an excel file but there are some blocks of zeroes I want to identify. Ex. [3,3,3,0,0,4,0,6,0,0,7]. I just want to run through the list so that I can print: first block of zeroes start at index 3 and end at index 4, second block etc.

The list is a single column in excel, and spans thousands of rows.

The way I am doing that is with the following code:

r = 1
while r+1000 < y:
   for q in range(r,y-1):
      if sheet.cell_value(q,3) == '0':
          start = q
          print(str(start))
          for e in range(q,y-1):
              if sheet.cell_value(e,3) != 0:
                  r = e
                  print(str(end))
                  break
          break

The prints are there so I can check if it is going right. But what's happening now in the output is it never stops and only prints the start value.

Isaac Naim
  • 11
  • 2
  • 1
    Don't use nested loops. Instead, just run through all the rows once, but use a variable to remember between rows whether or not you're within a block of zeroes. – Karl Knechtel Apr 05 '20 at 21:43
  • could you please give me a quick example of what you mean? how would you make a variable to remember blocks of zeroes? – Isaac Naim Apr 05 '20 at 21:45
  • 1
    All the variable's value needs to be is a boolean, "are we currently in a block of zeroes or not?". Come up with a rule that looks at the current value of the variable, and whether or not the current row is a zero, and decides whether we are still in the block, and if we should print a start or an end value. – Karl Knechtel Apr 05 '20 at 21:47
  • while troubleshooting this, I would add something like `iterations = 0 while(r+1000 < y) and (iterations < 10000):` and have `iterations += 1` in the code. that way, if your intended end, based on `r` and `y` doesn't come to pass, you will break out at some point. – JL Peyret Apr 05 '20 at 21:48
  • Haha I hate to keep bugging you, you've given me helpful information so far, but IM not there yet. Maybe you could be more specific to my example? – Isaac Naim Apr 05 '20 at 21:50
  • Where is the variable `end` defined? Is it meant to be `e`? – Swazy Apr 05 '20 at 21:50
  • 2
    To simplify a lot your problem you should also read your excel file and convert it into a "python" format. For this case, you could use a list or a numpy array and look for consecurive zeros, e.g. https://stackoverflow.com/questions/24885092/finding-the-consecutive-zeros-in-a-numpy-array – Mathieu Apr 05 '20 at 21:50
  • 2
    Thank you so much @Mathieu! That question solves my problem completely! How do I give you credit? – Isaac Naim Apr 05 '20 at 21:56
  • @IsaacNaim As it is not an answer but a comment, and as your question is actually already answered elsewhere, you can't. Glad I could help, have a good evening :) – Mathieu Apr 05 '20 at 22:30

1 Answers1

2

You could do something similar to this to get a listing of where every group of 0's start and end.

>>> li = [3,3,3,0,0,4,0,6,0,0,7]
>>> tracking_zeros = False
>>>
>>> for i, n in enumerate(li):
...     if n == 0:
...         if not tracking_zeros:
...             print(f"start zeros: {i}")
...             tracking_zeros = True
...     else:
...         if tracking_zeros:
...             print(f"  end zeros: {i}")
...             tracking_zeros = False
...             
start zeros: 3
  end zeros: 5
start zeros: 6
  end zeros: 7
start zeros: 8
  end zeros: 10

Not counting single zeroes:

>>> tracking_zeros = False
>>>
>>> for i, n in enumerate(li):
...     if n == 0:
...         if not tracking_zeros and li[i+1] == 0:
...             print(f"start zeros: {i}")
...             tracking_zeros = True
...     else:
...         if tracking_zeros:
...             print(f"  end zeros: {i}")
...             tracking_zeros = False
...             
start zeros: 3
  end zeros: 5
start zeros: 8
  end zeros: 10

If the last item in the list is a 0, it'll throw an index error so you might need to account for that.

Could just append a non-zero value at the end before you do the loop to prevent that: li.append(1)

Todd
  • 4,669
  • 1
  • 22
  • 30