我如何通过元组元素的函数在python参数呢? [英] How do I pass tuples elements to a function as arguments in python?

查看:235
本文介绍了我如何通过元组元素的函数在python参数呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,包括元组,我想每一个元组的元素传递给一个函数作为参数:

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,只需在列表循环做,用图示操作符( * ),以把它解析为函数的参数:

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

这篇关于我如何通过元组元素的函数在python参数呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆