gpt4 book ai didi

python - 如何将元组元素作为参数传递给函数?

转载 作者:太空狗 更新时间:2023-10-29 17:39:25 48 4
gpt4 key购买 nike

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

mylist = [(a, b), (c, d), (e, f)]

myfunc(a, b)
myfunc(c, d)
myfunc(e, f)

我该怎么做?

最佳答案

这在 Python 中实际上非常简单,只需遍历列表并使用 splat 运算符 (*) 将元组解包为函数的参数:

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 - 如何将元组元素作为参数传递给函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10625220/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com