本文介绍了如何将元组元素作为参数传递给函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个由元组组成的列表,我想将每个元组的元素作为参数传递给函数:
I have a list consisting of tuples, I want to pass each tuple's elements to a function as arguments:
mylist = [(a, b), (c, d), (e, f)]
myfunc(a, b)
myfunc(c, d)
myfunc(e, f)
我该怎么做?
推荐答案
实际上,这在Python中非常简单,只需遍历列表并使用splat运算符(*
)即可将元组解压缩为功能:
This is actually very simple to do in Python, simply loop over the list and use the splat operator (*
) to unpack the tuple as arguments for the function:
mylist = [(a, b), (c, d), (e, f)]
for args in mylist:
myfunc(*args)
例如:
>>> numbers = [(1, 2), (3, 4), (5, 6)]
>>> for args in numbers:
... print(*args)
...
1 2
3 4
5 6
这篇关于如何将元组元素作为参数传递给函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!