[英]Variable number of arguments in python
def func(*arg):
for i in arg:
print(i)
Here if a user is allowed to enter the arguments using input function (python 3.x). 在这里,如果允许用户使用输入函数(python 3.x)输入自变量。
Eg : "Hello,World,Good,Morning"
例如:
"Hello,World,Good,Morning"
If we want each word which is seperated by comma must be passed as argument... 如果我们希望每个用逗号分隔的单词都必须作为参数传递...
Eg: func("Hello",World","Good","Morning")
例如:
func("Hello",World","Good","Morning")
How can I solve this problem? 我怎么解决这个问题?
Use *
during function call too. 在函数调用期间也使用
*
。 str.split
splits the string into a list and then unpack the list items into individual arguments using *
. str.split
将字符串拆分为一个列表,然后使用*
将列表项解压缩为单个参数。
>>> strs = "Hello,World,Good,Morning"
>>> spl = strs.split(',')
>>> spl
['Hello', 'World', 'Good', 'Morning']
>>> func(*spl)
Hello
World
Good
Morning
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.