Python 将字符串拆分或提取为函数的参数?

Python 将字符串拆分或提取为函数的参数?,python,string,Python,String,假设我有一个接受字符串参数的函数。但我想动态地生成它们。似乎没有一种方法可以轻易地将其插入。这是怎么做到的?看到我的例子了吗 i_take_strings('one', 'two', 'and_the_letter_C') s = 'one two and_the_letter_c' i_take_strings(x for x in s.split()) #python thinks I'm retarded with this attempt s.split()已经返回了一个列表,因此

假设我有一个接受字符串参数的函数。但我想动态地生成它们。似乎没有一种方法可以轻易地将其插入。这是怎么做到的?看到我的例子了吗

i_take_strings('one', 'two', 'and_the_letter_C')

s = 'one two and_the_letter_c'

i_take_strings(x for x in s.split()) #python thinks I'm retarded with this attempt
s.split()
已经返回了一个列表,因此您可以通过在
*
前面加前缀将其传递给函数,如下所示:

i_take_strings(*s.split())

i_take_string(*tuple([x代表x在s.split()])就可以了,您要做的是创建一个生成器,而不是一个列表。@mission.liao代码可以工作,但可以简化为
i_take_string(*s.split())
。更多信息,Python不应该认为你是弱智。您只需告诉它将一个生成器传递给函数,python就可以做到这一点,而不会质疑您的心理健康。非常好。今天我学习了Python的一些新知识。和我一起快乐。