python - python中函数内部的参数
问题描述
我正在尝试解决有关参数的两个函数的问题。具体来说,我有以下问题:
def remove_stopwords(text):
list_words=[]
stop_words = (stopwords.words('italian'))
#bla bla bla
return(' '.join(t))
def clean(file):
# bla bla bla
file['C'] = file['Text'].apply(remove_stopwords())
# bla bla bla
return
然后当我按如下方式调用函数时:
clean(df)
它运作良好。但是,我想做这样的事情:
language='italian'
clean(df, language)
其中语言应该是放置在这里的字符串:
def remove_stopwords(text):
list_words=[]
stop_words = (stopwords.words(str(language)) # <--
...
问题是这个函数在 clean() 里面,当我尝试运行它时,我得到一个关于参数的错误。
您能告诉我如何在上述函数中正确编写参数吗?
解决方案
我相信 lambda 函数会做你正在尝试的事情。像这样的东西:
def remove_stopwords(text, language):
list_words=[]
stop_words = (stopwords.words(language))
return(' '.join(t))
def clean(file, language):
file['C'] = file['Text'].apply(lambda x: remove_stopwords(x, language))
return file['C']
clean(file, 'italian')
推荐阅读
- mysql - SQL - 将自定义属性表的数据加入 SELECT 中的主表
- java - Java中的双重比较失败
- ios - 如何在 iOS 中使用 Alamofire 和 Decodable 为嵌套 JSON 响应配置模型类
- scilab - 如何绘制函数 4(x)^2 = ((y)^2/(1-y))?
- google-cloud-run - Google Cloud Run 与本地机器相比非常慢
- xml - 使用 Shell 脚本将 XML 文件的标签及其值存储在两个不同的数组中
- java - 如何使用应用于他们的 box2d 物理在舞台上渲染演员?
- javascript - 从网络摄像头捕获图像并在 javascript 中转换为 base64
- r - 在 R 中组合数据进行回归
- python - 如何在 django rest 中自动继承外键模型的字段?