将值列表传递给函数

Gus*_*sto 1 python

抱歉这个愚蠢的问题,但坐在comp前面好几个小时让我的头过热,换句话说 - 我完全糊涂了.我的任务是定义一个函数,它接受一个单词列表并返回一些东西.如何定义一个将采用单词列表的函数?

def function(list_of_words):
    do something
Run Code Online (Sandbox Code Playgroud)

在Python IDLE中运行此脚本时,我们应该编写如下内容:

>>> def function('this', 'is', 'a', 'list', 'of', 'words')
Run Code Online (Sandbox Code Playgroud)

但是函数接受一个参数的Python错误和六个(参数)给出了.我想我应该给我的列表一个变量名,即list_of_words = ['this', 'is', 'a', 'list', 'of', 'words']但是...... 怎么样?

小智 9

使用代码:

def function(*list_of_words):
     do something
Run Code Online (Sandbox Code Playgroud)

list_of_words将是传递给函数的参数元组.


小智 6

只需调用您的函数:

function( ['this', 'is', 'a', 'list', 'of', 'words'] )
Run Code Online (Sandbox Code Playgroud)

这是将列表作为参数传递.