如何拆分列表并将其作为单独的参数传递? [英] How to split list and pass them as separate parameter?

查看:85
本文介绍了如何拆分列表并将其作为单独的参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我在列表中有值.我想分离这些值并将其作为单独的参数发送.

My problem is I have values in a list. And I want to separate these values and send them as a separate parameter.

我的代码是:

def egg():
    return "egg"

def egg2(arg1, arg2):
    print arg1
    print arg2

argList = ["egg1", "egg2"]
arg = ', '.join(argList)

egg2(arg.split())

这行代码(egg2(arg.split()))不起作用,但是我想知道是否可以调用一些内置函数来将值从列表中分离出来,因此以后我们可以将它们作为两个不同的参数发送.与egg2(argList[0], argList[1])类似,但要动态完成,因此不必显式键入列表参数.

This line of code (egg2(arg.split())) does not work, but I wanted to know if it is possible to call some built-in function that will separated values from list and thus later we can send them as two different parameters. Similar to egg2(argList[0], argList[1]), but to be done dynamically, so that I do no have to type explicitly list arguments.

推荐答案

>>> argList = ["egg1", "egg2"]
>>> egg2(*argList)
egg1
egg2

在调用函数时,可以使用* args(参数)和** kwargs(用于关键字参数). 看看此博客关于如何正确使用它.

You can use *args (arguments) and **kwargs (for keyword arguments) when calling a function. Have a look at this blog on how to use it properly.

这篇关于如何拆分列表并将其作为单独的参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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