python - 如何检查某些元素是否在python的代码中
问题描述
这就是我的意思,假设我正在检查某个电话号码属于哪个电话接线员
tel = input("Enter telephone number: ")
# Say I want to check if 077, #0782, 0783, 070 are in the user input
# Say the telephone numbers [0782123123, 078312312, 07712312]
# which belong to Operator1 are entered by the user
# I want be able to sort out the user's input
if 0783, 077, 070,0782 are in tel:
Then print ("This number belongs to Operator 1")
and if 0755, 0723 are in tel:
Then print ("This number belongs to Operator 2")
解决方案
您可以使用内置功能any
:
tel = input("Enter telephone number: ")
if any(num in tel for num in ('0783', '077', '070','0782')):
print ("This number belongs to Operator 1")
if any(num in tel for num in ('0755', '0723')):
print ("This number belongs to Operator 2")
推荐阅读
- python - 使用 Python 在 Raspberry-Pi 中编辑 /etc/network 下的接口文件
- html - 为什么 flex 网格中的可滚动表会在列容器之外中断?
- css - Boostrap 表单的垂直对齐问题
- c# - 一个逗号仅在泛型参数中意味着什么?
- angular - 如何找到添加到leafletLayer的多个标记的边界
- javascript - 我们可以在不使用图形 api 的情况下在 azure ad 中对用户进行身份验证吗
- r - 如何在 rmarkdown html_document 中对齐表格和绘图
- rest - RESTful API 做同一件事的不同方式
- c++ - 如何在 GLFW 窗口中限制我的每秒帧数?(使用亲爱的 ImGui)
- sql - 我丢失了 Azure SQL 数据库,但我有 .bacpac 文件。我可以以某种方式打开它吗?