python - 如何在 python 中使用 if else 语句将列表放入函数中以创建变量?
问题描述
我如何让这些列表通过我的函数(这是正确的术语)?
列表:
temp_a = [1, 2, 3, 4, 5]
temp_b = [2, 2, 3, 4, 1]
temp_c = [1, 0, 2, 2, 1]
函数(这里我只是放了*,但我想在函数中定义列表):
def temp_class(temperature):
temp_class = []
for i in *:
if i < 3:
temp_class.append(0)
else:
temp_class.append(1)
不起作用的示例:
temp_value = [tempclass(t) for t in temp_a] #initial function is wrong
print(temp_value)
这里有什么问题?我试图查看 Stack Overflow 和其他地方,但找不到任何东西。也许我使用了错误的术语。
我用什么代替*?
解决方案
我猜你正在寻找星号参数:
def foo(*args, **kwargs):
print(args)
print(kwargs)
测试:
foo([1,2,3], bar1="foobar", bar2="foobar2" )
([1, 2, 3],)
{'bar1': 'foobar', 'bar2': 'foobar2'}
推荐阅读
- laravel - 如何在 Laravel 中更改用户数据
- ios - iOS Firebase 身份验证域对用户可见
- python - Python 将网站中的所有内容获取到 html 文件中
- rxjs - NGXS 连续触发效果
- php - 如何用未知字符替换路径和文件名字符串?
- java - JavaFX Gridpane 搜索返回空节点
- python-3.x - Python - 没有名为“系统”的模块
- javascript - 使用 gatsby-image 插件优化所有博客文章图片
- sql-server - 插入行时出现标识值的 SQL Server 错误
- sql - update и ошибка 子查询返回超过 1 个值