python中怎么求和
python自带的sum函数(或者Numpy中的sum函数),
无参时,所有全加;
axis=0,按列相加;
axis=1,按行相加
import numpy as np #python中自带的sum print(sum([[1,2,3],[4,5,5]])) print(sum([[1,2,3],[4,5,5]],axis=0)) print(sum([[1,2,3],[4,5,5]],axis=1)) #Numpy中的sum a = np.sum([[1,2,3], [4,5,5]]) #无参 print(a) print(a.shape) a = np.sum([[1,2,3], [4,5,5]],axis=0) #axis=0, 按列相加 print(a) print(a.shape) a = np.sum([[1,2,3], [4,5,5]],axis=1) #axis=1, 按行相加 print(a) print(a.shape)
输出结果
20 [5 7 8] [ 6 14] 20 () [5 7 8] (3,) [ 6 14] (2,)
学习python,尽在云海天教程网!
来源:PY学习网:原文地址:https://www.py.cn/article.html