python - 在自定义函数参数中将列表的值设为布尔值
问题描述
我有一个给定的列表和一个带有一些参数的自定义函数。该列表包含字符串形式的参数,我想将这些参数更改为 True 如果列表包含 else 在函数中保持错误。
这是示例:
list_params = ['remove_digits','remove_stopwords','clean_data']
custom_function(remove_digits =False, clean_data =False, remove_stopwords = False, text_lemmatization =False)
这里所有参数首先为假,但一旦列表包含这些参数,在函数中将它们选择为 True,否则保持为假。我想要all the parameters to True at once if present in list
。
解决方案
假设您的函数定义类似于:
def custom_function(remove_digits=False, clean_data=False, remove_stopwords=False, text_lemmatization=False):
所以你想“打开”列表中的参数。您可以通过将列表转换为kwargs
dict并将其解压缩到函数调用来做到这一点:
list_params = ['remove_digits','remove_stopwords','clean_data']
dict_params = {param: True for param in list_params}
custom_function(**dict_params)
推荐阅读
- oracle - 将 oracle 表中的前导零提取到文件中
- java - 预览 Google 幻灯片/PowerPoint
- arrays - 屏蔽多维数组 - numpy
- r - 有没有办法将公式应用于两个值向量并获取每个组合的输出数据框?
- ubuntu-18.04 - libVLC 未在 Ubuntu 18.04 (WSL 2.0) 上初始化
- node.js - 来自 MD5 库的不同结果
- database - 基于总天数的每小时平均行数
- c - Swift:将已分配的字符数组转换为字符指针
- java - websphere 缓存中对象数组的 cachespec.xml
- bash - sed 输出 url 中两个匹配字符串之间的值