将列表传递给函数以充当多个参数 [英] Pass a list to a function to act as multiple arguments
本文介绍了将列表传递给函数以充当多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我认为解压"可能是错误的词汇-抱歉,我确定这是一个重复的问题.
I think 'unpack' might be the wrong vocabulary here - apologies because I'm sure this is a duplicate question.
我的问题很简单:在一个需要项目列表的函数中,如何在不出错的情况下传递Python列表项目?
My question is pretty simple: in a function that expects a list of items, how can I pass a Python list item without getting an error?
my_list = ['red', 'blue', 'orange']
function_that_needs_strings('red', 'blue', 'orange') # works!
function_that_needs_strings(my_list) # breaks!
肯定有一种方法可以扩展列表,并在蹄上传递函数'red','blue','orange'
?
Surely there must be a way to expand the list, and pass the function 'red','blue','orange'
on the hoof?
推荐答案
function_that_needs_strings(*my_list) # works!
这篇关于将列表传递给函数以充当多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文