Mau*_*dez 0 python functional-programming python-2.x python-2.7
我想传递一个参数元组args(由于某些其他函数,元组长度的变化)到一个函数,它的工作原理
def myf(x1,x2,x3,....)
return something involving x1,x2....
args = (y1,y2,.....)
call myf with args as myf(y1,y2,....)
Run Code Online (Sandbox Code Playgroud)
你是如何实现这一目标的?在我的实际问题,我有工作sympy的功能myf实际上是reshape和变量参数列表args是通过获得一些ND-阵列,比如形状产生一个元组A,所以args = A.shape.最后,我想B根据形状重塑另一个数组A.最小的例子是
from sympy import *
A = Array(symbols('a:2:3:4:2'),(2,3,4,2))
B = Array(symbols('b:8:3:2'),(8,3,2))
args = A.shape
print args
print B.reshape(2,3,4,2) # reshape(2,3,4,2) is the correct way to call it
print B.reshape(args) # This is naturally wrong since reshape((2,3,4,2)) is not the correct way to call reshape
Run Code Online (Sandbox Code Playgroud)
使用参数解包:
def myf(x1,x2,x3):
return x1 + x2 + x3
args = (1, 2, 3)
myf(*args) # 6
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
134 次 |
| 最近记录: |