Python 如何输入参数以列表或元组的形式运行?

Python 如何输入参数以列表或元组的形式运行?,python,list,function,parameters,Python,List,Function,Parameters,是否可以以列表的形式输入函数的参数。 例如— list1 = ["somethin","some"] def paths(list): import os path = os.path.join() #I want to enter the parameters of this function from the list1 return path 好的,我得到了我的答案,但只是一个传统的问题,仅与此相关- 这是我的密码- def files_check(file_na

是否可以以列表的形式输入函数的参数。 例如—

list1 = ["somethin","some"]
def paths(list):
    import os
    path = os.path.join() #I want to enter the parameters of this function from the list1
    return path
好的,我得到了我的答案,但只是一个传统的问题,仅与此相关- 这是我的密码-

def files_check(file_name,sub_directories):
    """
        file_name :The file to check
        sub_directories :If the file is under any other sub directory other than the   application , this is a list.
    """
    appname = session.appname
    if sub_directories:
        path = os.path.join("applications",
                        appname,
                        *sub_directories,
                         file_name)
        return os.path.isfile(path)
    else:
         path = os.path.join("applications",
                        appname,
                        file_name)
         return os.path.isfile(path)
我得到了这个错误-

 SyntaxError: only named arguments may follow *expression

请帮帮我。

只需使用
*
操作符将列表解包即可

path = os.path.join(*list) 
您可以使用splat运算符(
*
):

演示:

>>> import os
>>> lis = ['foo', 'bar']
>>> os.path.join(*lis)
'foo\\bar'
更新:

>>> import os
>>> lis = ['foo', 'bar']
>>> os.path.join(*lis)
'foo\\bar'
要回答新问题,一旦在参数中使用了
*
,就不能传递位置参数,可以在此处执行类似操作:

from itertools import chain

def func(*args):
    print args

func(1, 2, *chain(range(5), [2]))
#(1, 2, 0, 1, 2, 3, 4, 2)

不要使用
列表
作为变量名

您可以使用*-运算符

比如说

data=['a','b']os.path.join(*data)

给予

'a/b'

嘿Ashwini你能帮我做新的吗question@Anurag-Sharma一旦使用了
*
,就不能传递位置参数。因此,
*子目录
之后的
文件名
抛出了错误。@Anurag Sharma您可以使用
itertools.chain
那里:
os.path.join(“应用程序”,appname,*chain(子目录,[文件名])