Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取列表/元组的每个元素,其中列表作为参数传递给python中的函数?_Python_Python 2.7 - Fatal编程技术网

如何获取列表/元组的每个元素,其中列表作为参数传递给python中的函数?

如何获取列表/元组的每个元素,其中列表作为参数传递给python中的函数?,python,python-2.7,Python,Python 2.7,下面是无法打印元组项的代码段。[使用python版本2.6.8/2.7.10] def lists (var, *st): print type(st) for item in range(1,len(st)): print "Items is:" + item st = ['udf','var_udf'] lists("a",st) 提前感谢这不会打印任何内容,因为您使用了范围(1,len(st))中的项将项的值设置为整数。相反,你可以这样做: for ite

下面是无法打印元组项的代码段。[使用python版本2.6.8/2.7.10]

def lists (var, *st):
    print type(st)
    for item in range(1,len(st)):
        print "Items is:" + item
st = ['udf','var_udf']
lists("a",st)

提前感谢

这不会打印任何内容,因为您使用了范围(1,len(st))中的
将项的值设置为整数。相反,你可以这样做:

for item in st:
代码:


如果将列表作为参数传递给函数,下面的函数将打印列表中的每个元素。如果您想执行任何操作,如append等,您可以在
之后添加代码,用于
循环。

使用
print''尝试可能的重复。加入(i)
以使其更干净问题是“如何获取列表或元组中列表作为参数传递给python函数的每个元素?”谢谢vaibhav。但是,我的实际需求是使用该元组中的ITME获取文件名。与此类似的是create_udf.sql/create_var_udf.sql,在st:sql=“create_.join(item)+.sql“print sql st=['udf','var_udf']列表(“a”,st)中为项列出(var,*st):打印类型(st)将输出设置为:udfcreate_var_udf.sql能否请您帮助实现两个sql文件名然后确保st的第一个元素始终是
'
,并将
'/'
附加到除最后一个元素之外的所有元素。
def lists (var, *st):
    print type(st)
    for item in st:
        print "Items is:"
        print ' '.join(item)
st = ['','udf/','var_udf']
lists("a",st)
a = [1,2,3,4,5,6,7,8,9] #list
print (a)

def b(list):
    for i in list:
        print (i) #each element of the list printed here

b(a) #calling def