Python - 如何在数组

时间:2017-06-14 08:01:04

标签: python arrays

如果我有一个数组,让我们说:np.array([4,8,-2,9,6,0,3,-6])我想将之前的数字添加到下一个元素,我该怎么办? 每次数字0都显示添加元素“重启”。

上面这个数组的例子,我运行函数时应该得到以下输出:

如果在事务中插入上述数组,

stock = np.array([4,12,10,19,25,0,3,-3])是正确的输出。

def cumulativeStock(transactions):

    # insert your code here

    return stock

我想不出解决这个问题的方法。任何帮助都将非常感激。

5 个答案:

答案 0 :(得分:2)

我相信你的意思是这样的?

z = np.array([4,8,-2,9,6,0,3,-6])
n = z == 0
    [False False False False False  True False False]
res = np.split(z,np.where(n))
    [array([ 4,  8, -2,  9,  6]), array([ 0,  3, -6])] 
res_total = [np.cumsum(x) for x in res]
    [array([ 4, 12, 10, 19, 25]), array([ 0,  3, -3])]
np.concatenate(res_total)
    [ 4 12 10 19 25  0  3 -3]

答案 1 :(得分:0)

另一个矢量化解决方案:

import numpy as np
stock = np.array([4, 8, -2, 9, 6, 0, 3, -6])

breaks = stock == 0
tmp = np.cumsum(stock)
brval = numpy.diff(numpy.concatenate(([0], -tmp[breaks])))
stock[breaks] = brval
np.cumsum(stock)
# array([ 4, 12, 10, 19, 25,  0,  3, -3])

答案 2 :(得分:0)

import numpy as np
stock = np.array([4, 12, 10, 19, 25,  0,  3, -3, 4, 12, 10, 0, 19, 25,  0,  3, -3])

def cumsum_stock(stock):
    ## Detect all Zero's first 
    zero_p = np.where(stock==0)[0]
    ## Create empty array to append final result  
    final_stock = np.empty(shape=[0, len(zero_p)])
    for i in range(len(zero_p)):
        ## First Zero detection
        if(i==0):
             stock_first_part = np.cumsum(stock[:zero_p[0]])
             stock_after_zero_part  = np.cumsum(stock[zero_p[0]:zero_p[i+1]])
             final_stock = np.append(final_stock, stock_first_part)
             final_stock = np.append(final_stock, stock_after_zero_part) 
        ## Last Zero detection
        elif(i==(len(zero_p)-1)):  
             stock_last_part = np.cumsum(stock[zero_p[i]:])
             final_stock = np.append(final_stock, stock_last_part, axis=0)
        ## Intermediate Zero detection
        else:
            intermediate_stock = np.cumsum(stock[zero_p[i]:zero_p[i+1]])
            final_stock = np.append(final_stock, intermediate_stock, axis=0)
    return(final_stock)


final_stock = cumsum_stock(stock).astype(int)

#Output
final_stock
Out[]: array([ 4, 16, 26, ...,  0,  3,  0])

final_stock.tolist()
Out[]: [4, 16, 26, 45, 70, 0, 3, 0, 4, 16, 26, 0, 19, 44, 0, 3, 0]

答案 3 :(得分:0)

def cumulativeStock(transactions):

    def accum(x):
        acc=0
        for i in x: 
           if i==0:
              acc=0
           acc+=i
           yield acc

    stock = np.array(list(accum(transactions)))
    return stock

输入np.array([4,8,-2,9,6,0,3,-6]) 它返回 array([ 1, 3, 6, 9, 13, 0, 1, 3, 6])

答案 4 :(得分:-1)

我认为你的意思是你想在每个零点分开列表?

from itertools import groupby
import numpy


def cumulativeStock(transactions):

#split list on item 0
groupby(transactions, lambda x: x == 0)
all_lists = [list(group) for k, group in groupby(transactions, lambda x: x ==  0) if not k]

# cumulative the items
stock = []
for sep_list in all_lists:
    for item in numpy.cumsum(sep_list):
        stock.append(item)

return stock


print(cumulativeStock([4,8,-2,9,6,0,3,-6]))

将返回: [4,12,10,19,25,3,-3]