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)
How do I do it?
Best Regards
解决方案
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)
E.g:
>>> numbers = [(1, 2), (3, 4), (5, 6)]
>>> for args in numbers:
... print(*args)
...
1 2
3 4
5 6