2017-06-14 157 views
0

如果我有一個數組,我們假設:np.array([4,8,-2,9,6,0,3,-6])並且我想將前一個數字添加到下一個元素,我該怎麼辦? 每當數字0顯示元素「重新啓動」的添加時。Python - 如何添加和減去數組中的元素

stock = np.array([4,12,10,19,25,0,3,-3])是正確的輸出,如果上述陣列被插入交易:

與上述陣列的一個例子,當我運行功能我應該得到以下輸出。

def cumulativeStock(transactions): 

    # insert your code here 

    return stock 

我想不出一種方法來解決這個問題。任何幫助將非常感激。

+0

不確定你在問什麼 –

+4

['numpy.cumsum'](https://docs.scipy.org/doc/numpy/reference/generated/numpy.cumsum.html)? –

+0

請顯示您正在使用的代碼...「並且每當數字0顯示添加元素的重新啓動」「。你如何看待這個?無論如何,請檢查Nils Werner的答案。我認爲這是正確的。 – Sanchit

回答

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 ... –

+0

@IgnacioVergaraKausel woops,你是對的,hihi – zwep

-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]

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]) 
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] 
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]) it returns array([ 1, 3, 6, 9, 13, 0, 1, 3, 6])

+0

當我運行這個我沒有得到任何輸出。我收到此消息「名稱」a'未定義「。但是這是我希望函數返回的輸出。 –

+0

哎呦做了一個編輯,一個是我的測試變量 – suvy

+0

這實際上工作!非常感謝。 –