簡體   English   中英

如果條件在 Numpy Sum 中,我如何使用

[英]How can i use if condition in Numpy Sum

我有下面的數組顯示

arr =np.array([85,22,3,24,56,89,84,15,1,45,1,40,65])

我想做小於 25 的總和。

我知道我能做到

total_price = 0
for x in arr:
    if x < 25:
        total_price += x

but is there something like sum(arr, i <25) which will return total sum value which is less than 25?in c++ there lambda function can i do such thing in python?how can i use numpy sum? 使用 numpy 庫更好嗎?

嘗試:

import numpy as np

arr = np.array([85,22,3,24,56,89,84,15,1,45,1,40,65])

>>> arr[arr<25].sum()

66

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM