python - 向“或”语句添加参数的更简单方法?
问题描述
有没有更简单的方法可以在我不必写出来的情况下执行以下操作outputbodypixel == everytime
?理想情况下,我想从一个列表中提取数据,我可以在其中添加#ebebeb
, #ececec
, #212121
.
if(outputbodypixel == "#356044" or outputbodypixel == "#22402b" or
outputbodypixel == "#213f2c" or outputbodypixel == "#356043" or
outputbodypixel == "#22402c" or outputbodypixel == "#346044"):
output_body = "green"
elif(outputbodypixel == "#7c3d15" or outputbodypixel == "#493613" or
outputbodypixel == "#4a3612" or outputbodypixel == "#6a553e" or
outputbodypixel == "#785735" or outputbodypixel == "#5e4b37" or
outputbodypixel == "#6a553e" or outputbodypixel == "#86623c" or
outputbodypixel == "#8b4f0d" or outputbodypixel == "#7c3d14" or
outputbodypixel == "#6a553d" or outputbodypixel == "#7c3d14" or
outputbodypixel == "#6a553d" or outputbodypixel == "#7c3d14" or
outputbodypixel == "#87623c" or outputbodypixel == "#6e3612" or
outputbodypixel == "#87613c"):
output_body = "brown"
elif(outputbodypixel == "#8b8b8a" or outputbodypixel == "#8b8b8b" or
outputbodypixel == "#8b8a8b"):
output_body = "silver"
else:
output_body = "NOT DEFINED"
我正在使用 Python 3.x。
解决方案
只需使用每个条件的所有可能值的元组(或列表,如果您已经为每种情况都有一个),这与询问一个值是否是另一个值等in
完全相同。outputbodypixel
or
or
if outputbodypixel in ("#356044", "#22402b", "#213f2c", "#356043", "#22402c", "#346044"):
output_body = "green"
elif outputbodypixel in ("#7c3d15", "#493613", "#4a3612", "#6a553e", "#785735", "#5e4b37", "#6a553e", "#86623c", "#8b4f0d", "#7c3d14", "#6a553d", "#7c3d14", "#6a553d", "#7c3d14", "#87623c", "#6e3612", "#87613c"):
output_body = "brown"
elif outputbodypixel in ("#8b8b8a", "#8b8b8b", "#8b8a8b"):
output_body = "silver"
else:
output_body = "NOT DEFINED"
推荐阅读
- vue.js - 当发生 Vuex 突变时被通知
- java - 如何将数据库中的值存储到集合中?
- freepascal - Lazarus 中 ListBox lbVirtual 的问题
- php - delete_cookies() 助手在 codeigniter 4 中不起作用
- python - 在 HTML 代码中下载和查找特定字符串
- python - 如何使用带有多 GPU 的变压器管道?
- javascript - 如何使用javascript提醒所有p?
- python - 具有半正弦距离的 DBSCAN 聚类
- .net - 如何使用 Visual Studio(可能是 MsBuild)在特定目标目录中获取构建输出
- java - Java 随机百分比机会