Summation of only consecutive values in a python array
Question: I am new on python (and even programing!), so I will try to be as clear as I
can to explain my question. For you guys it could be easy, but I have not
found a satisfactory result on this yet.

Here is the problem:

I have an array with both negative and positive values, say:

    
    
    x = numpy.array([1, 4, 2, 3, -1, -6, -6, 5, 6, 7, 3, 1, -5, 4, 9, -5, -2, -1, -4])
    

I would like to sum ONLY the **negative** values that are **continuous** ,
i.e. only _sum_(-1, -6, -6), _sum_(-5, -2, -1, -4) and so on. I have tried
using _numpy.where_ , as well as _numpy.split_ based on the condition.

For example:

    
    
     for i in range(len(x)):
         if x[i] < 0.:
             y[i] = sum(x[i])
    

However, as you can expect, I just got the summation of all negative values in
the array instead. In this case **sum**(-1, -6, -6, -5, -5, -2, -1, -4) Could
guys share with me an aesthetic and efficient way to solve this problem? I
will appreciate any response on this.

Thank you very much


Answer: You can use [`itertools`](https://docs.python.org/3/library/itertools.html)
module, here with using `groupby` you can grouping your items based on those
sign then check if it meet the condition in `key` function so it is contains
negative numbers then yield the sum else yield it and at last you can use
`chain.from_iterable` function to chain the result :

    
    
    >>> from itertools import groupby,tee,chain
    >>> def summ_neg(li):
    ...     for k,g in groupby(li,key=lambda i:i<0) :
    ...           if k:
    ...              yield [sum(g)]
    ...           yield g
    ... 
    >>> list(chain.from_iterable(summ_neg(x)))
    [1, 4, 2, 3, -13, 5, 6, 7, 3, 1, -5, 4, 9, -12]
    

Or as a more pythonic way use a list comprehension :

    
    
     list(chain.from_iterable([[sum(g)] if k else list(g) for k,g in groupby(x,key=lambda i:i<0)]))
    [1, 4, 2, 3, -13, 5, 6, 7, 3, 1, -5, 4, 9, -12]
    

