社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

Lewis Lockhart • 4 年前 • 278 次点击  

问:如何在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
 
278 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Wasif Hasan
Reply   •   1 楼
Wasif Hasan    4 年前

if !

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

如果 or 如果