我试图使用单个元组将多个参数传递给函数。这种奇怪方法的原因是我的实际程序跟随this example将多个参数传递给multiprocessing
pool.map
。
问题是该示例仅使用两个参数,但我需要更多。当我真的只想要一个包含多个条目的元组时,我最终传递了两个 list 参数。
我最终不得不重新定义函数中的各个变量(例如rowTest = data_to_check[0]
)。但这很丑陋而且很麻烦。请问有什么更好的方法呢?
这是一个简化的例子:
import itertools
def worker(data_to_check, support_data):
# UGLY -- do not want to have to reassign internally
rowTest = data_to_check[0]
rowProd = data_to_check[1]
countRow = support_data[0]
precision = support_data[1]
print 'data:', rowTest, rowProd
print 'params:', countRow, precision
def worker_star(input_tuple):
"""Convert `f([1,2])` to `f(1,2)` call."""
print 'input_tuple:', input_tuple
return worker(*input_tuple)
def main():
# data arguments to pass - iterate through all pairs
groups_test = [ 1, 2, 3]
groups_prod = [-1,-2,-3]
groups_combined = zip(groups_test,groups_prod)
# supporting arguments to pass (repeat on each call)
countRow = 0
precision = 4
worker_params = [countRow, precision]
# producing a tuple of lists (just need a simple tuple?)
map(worker_star, itertools.izip( [ [x[0],x[1]] for x in groups_combined ], itertools.repeat(worker_params) ))
print 'done'
if __name__ == '__main__':
main()
答案 0 :(得分:0)
一个渐进的改进:
def worker(data_to_check, support_data):
rowTest, rowProd = data_to_check
countRow, precision = support_data
或者
groups_test = [ 1, 2, 3]
groups_prod = [-1,-2,-3]
countRows = [0]*4
precisions = [4]*4
groups_combined = zip(groups_test, groups_prod, countRows, precisions)
# [(1, -1, 0, 4), (2, -2, 0, 4), ...]
map(worker_star, groups_combined) # redefine worker_star to accept 4 parameters
答案 1 :(得分:0)
我发现this related example显示了如何传递2个以上的参数。基本上,我修改了辅助函数worker_star
以在传递之前展平元组:
import itertools
#def worker(data_to_check, support_data):
def worker(rowTest, rowProd, countRow, precision):
# UGLY -- do not want to have to reassign internally
# rowTest = data_to_check[0]
# rowProd = data_to_check[1]
# countRow = support_data[0]
# precision = support_data[1]
print 'data:', rowTest, rowProd
print 'params:', countRow, precision
def worker_star(input_tuple):
"""Convert `f([1,2])` to `f(1,2)` call."""
# flatten out the tuple before passing it on
input_tuple_flat = input_tuple[0] + input_tuple[1]
#return worker(*input_tuple)
return worker(*input_tuple_flat)
def main():
# data arguments to pass - iterate through all pairs
groups_test = [ [1], [2], [3]]
groups_prod = [[-1],[-2],[-3]]
groups_combined = zip(groups_test,groups_prod)
# supporting arguments to pass (repeat on each call)
countRow = 0
precision = 4
worker_params = (countRow, precision)
# producing a tuple of lists (just need a tuple?)
map(worker_star, itertools.izip( ( (x[0],x[1]) for x in groups_combined ), itertools.repeat(worker_params) ))
print 'done'
if __name__ == '__main__':
main()