Py学习  »  Python

在基本python列表理解中使用“or”运算符

Lewis Lockhart • 3 年前 • 198 次点击  

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

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

这些数字可以被5和7整除。我也试过:

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

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

use-of-or-operator-in-python-lambda-function

not-comprehending-list-comprehension-in-python

is-there-a-binary-or-operator-in-python-that-works-on-arrays

how-to-convert-this-my-code-into-a-list-comprehension

python-list-comprehension-with-multiple-ifs

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/99363
 
198 次点击  
文章 [ 1 ]  |  最新文章 3 年前
Wasif Hasan
Reply   •   1 楼
Wasif Hasan    3 年前

if !

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

如果 or 如果