在基本的 python 列表理解中使用 'or' 运算符

Using an 'or' operator in a basic python list comprehension

问题:如何在 python 列表理解中使用 OR?

我希望输出 0-99 之间的任何数字,该数字可以被 5 或 7 整除且没有余数。我有以下代码:

numbers = [x for x in range(99) if x % 5 == 0 if x % 7 == 0]

但是这个 returns: 0, 35, 70 这是可被 5 和 7 整除的数字。我也试过:

numbers = [x % 5 == 0 or x % 7 == 0 for x in range(99)]

但是每个数字的 returns 真或假,我希望自己获得数字。使用这个:

numbers = [x for x in range(99) if x % 5 == 0 or if x % 7 == 0]

抛出语法错误。

我查看了以下页面,但无法理解如何应用所提供的解决方案。他们每个人似乎都对我想要的解决方案提供了细微差别,但并不是我想要的。

datacamp.com/community/tutorials/python-list-comprehension

programiz.com/python-programming/list-comprehension

python-list-comprehension-with-multiple-ifs

不要使用另一个 if

numbers = [x for x in range(99) if (x % 5 == 0) or (x % 7 == 0)]
print(numbers)

因为if是一个语句,那些是表达式,然后做or,用if shorthand来检查。