arrays - 如何动态减去numpy数组
问题描述
我有一个方法可以返回一些像下面这样的 numpy 数组
def numpy_array():
......
......
return true, test_1, test_2, test_3
我有另一种方法可以计算下面true
的减法rest of the arrays
def subtraction():
true, test_1, test_2, test_3 = numpy_array()
sub_1 = np.subtract(true, test_1)
sub_2 = np.subtract(true, test_2)
......
......
return sub_1, sub_2
问题是,我的方法中可能有很多数组def numpy_array()
。我想以def subtraction()
动态的方式编写该方法。这样我就不需要手动减去数组(np.subtract(true, test_1)
,np.subtract(true, test_2)
等)。
你能告诉我我该怎么做吗?
解决方案
numpy_array
或任何具有多个参数的 python 函数将返回一个元组。然后,您可以遍历元组以获得所需的差异:
def subtraction():
matrices = numpy_array()
# a tuple of some number of numpy arrays
first = matrices[0]
# the first array, 'true' in question
result = [first - matrices[i] for i in range(1,len(matrices))]
# the difference between the first and subsequent arrays
return tuple(result)
推荐阅读
- java - 如何在会话中存储jsp页面的onclick值?
- mysql - MySQL View 代替子查询不返回相同的结果
- html - 如何调整导航栏按钮的大小并使其适合屏幕宽度?
- vue.js - 单击后导航栏(移动视图)不关闭 - Vuejs
- python - OpenCV在视频中查找冻结帧
- python - 如何将一个类元素连接到另一个类?
- javascript - 使用 setTimeout() 后将组件返回到原始状态或强制重新渲染
- python - 来自 3D 阵列的 3D 切片
- python - 将函数应用于熊猫数据框的列
- python-3.x - 'python3' 不被识别为内部或外部命令?