3

If I have a list like:

X = [0, 2, 3, 4.0, 1, 0, 3, 0, 0, 0, 2, 1, 5, 2, 6, 0, 2.2, 1]

How would I write code in python that takes this list and finds the number of consecutive positive numbers and then makes a list that has lists of each of those consecutive numbers in it.

for example this example of x would return a number 4 and it would also return:

[[ 2, 3, 4.0, 1], [3], [ 2, 1, 5, 2, 6], [ 2.2, 1]]. 

I wrote this to find all the zeros but I do not know where to go from there:

 zeros = []
 for i in range(0,len(World)):
      if z[i]==0: 
          zeros.append(i)
Mazdak
  • 105,000
  • 18
  • 159
  • 188
Laura
  • 53
  • 5
  • 1
    Can you explain what you are trying to do in more detail? – khelwood Oct 01 '15 at 14:31
  • I am trying to write a function in which I input a list and then return the amount of times consecutive numbers appear in that list and then return a list that contains lists of these numbers. – Laura Oct 01 '15 at 14:32
  • 3
    @Laura pro-tip: when someone asks for more detail, don't just use the same words in a slightly different order! – jonrsharpe Oct 01 '15 at 14:33
  • 1
    Oh, "consecutive positive numbers" means "*adjacent* positive numbers". That would have made much more sense. – khelwood Oct 01 '15 at 14:40
  • @Laura Give at least 3 examples with input and expected output. – Martin Thoma Oct 01 '15 at 14:53

2 Answers2

7

There is no need to fine the zeros. You can simply loop over your list and put the positive numbers in a temporary list until you encounter with a zero, but as a more pythonic approach for such tasks you can use itertools.groupby :

>>> from itertools import groupby
>>> [list(g) for k,g in groupby(X,key=lambda x:x>0) if k]
[[2, 3, 4.0, 1], [3], [2, 1, 5, 2, 6], [2.2, 1]]

If you want to do it without itertools module you can use following function which works based on preceding explanation and yields the temp list every time it encounter a zero, and at last returns a generator contains list of positive numbers which you can convert it to a list by calling the list function :

>>> def grouper(li,temp=[]):
...    for i in li:
...        if i>0:
...           temp.append(i)
...        else:
...            if temp: yield temp
...            temp=[]
...    if temp : yield temp
...

Demo:

>>> list(grouper(X))
[[2, 3, 4.0, 1], [3], [2, 1, 5, 2, 6], [2.2, 1]]
Mazdak
  • 105,000
  • 18
  • 159
  • 188
2

You do not need to find out the indices of zeroes before-hand. You can simply keep another temporary list holding the values that are positive (greater than 0) since the last time 0 was encountered. And then when you encounter a 0 , append that temporary list to the result list, and change that temporary list to a new list. Example -

result = []
temp = []
for i in X:
    if i <= 0:
        if temp:
            result.append(temp)
            temp = []
    else:
        temp.append(i)
if temp:
    result.append(temp)

Demo -

>>> result = []
>>> temp = []
>>> for i in X:
...     if i <= 0:
...         if temp:
...             result.append(temp)
...             temp = []
...     else:
...         temp.append(i)
...
>>> if temp:
...     result.append(temp)
...
>>> result
[[2, 3, 4.0, 1], [3], [2, 1, 5, 2, 6], [2.2, 1]]
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176