![](/img/trans.png)
[英]Is there any built-in function in python to minimize the sum of all y-deviations squared (Least-Square)?
[英]Built in function all() and any() in Python
我在使用all()的內置函數時遇到了麻煩。 下面的代碼應該顯示“失敗”,但是卻給我“成功”。 誰能告訴我為什么會這樣?
test = np.array([9.,-1.,2.,3.,5.])
if test[:].all() > 0.:
print 'success'
else:
print 'fail'
就像其他評論中提到的那樣, all()
是一個布爾函數,因此它只是在查找所有元素是否都不為0(即False
)。
這就是您要針對特定情況使用all()
的方式。 它使用生成器理解基於原始數組對True
和False
進行迭代。 如果任何元素小於或等於零,它將返回False
。
all(i > 0 for i in test)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.