IF 语句压缩多个 OR 语句

我有以下几行

if 'smoke' in row['product'].lower() or 'grill' in row['product'].lower() or 'choco' in row['product'].lower():

我想在 OR 子句中添加多个项目。['smoke', 'grill, 'choco', ...],如何在不丢失逻辑的情况下压缩 if 语句?


缥缈止盈
浏览 121回答 2
2回答

温温酱

一种方法是使用 anyif any(item in row['product'].lower()       for item in ('choco', 'smoke', 'grill'):或者 ifrow['product'].lower()是一个字符串并且您想比较它们是否相同:if row['product'].lower() in ('choco', 'grill', 'smoke'):在我有名称元组的地方,将其替换为预先编写的字符串列表,如下所示:names = ('choco', 'grill', 'smoke', ...)if row['product'].lower() in names:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python