python - 如何检查值是否存在于列表中,元素内用行分隔
问题描述
我需要检查值(例如13
or 17.5
)是否存在于字符串列表中定义的范围内,例如:
L = ["12-14", "15-16", "17-20"]
例如,范围是12
to 14
inclusive、15
to16
等。
我想:
if "13.5" in L:
print("yes")
else:
print("no")
预期输出:yes
我怎样才能做到这一点?
解决方案
使用.split()
,然后使用与列表中的项目进行比较any
lst = ["12-14", "15-16", "17-20"]
if any(int(i.split('-')[0]) < 13.5 < int(i.split('-')[1]) for i in lst):
print('yes')
else:
print('no')
# yes
推荐阅读
- javascript - 无法访问@font-face 中提到的字体文件
- shell - 如何在shell脚本的不同函数中增加计数器值
- amazon-web-services - 如何在 .NET Core AWS Lambda 包中包含来自 Nuget 的 exe 文件?
- wix - 如何在安装后将文件从 wix 安装程序项目复制到本地用户系统?
- python - 如何在此处使用 .format?
- string - 在for循环python pandas中通过多个条件提取字符串
- apache-spark - 有没有可能在不执行的情况下提取火花作业的 DAG 的方法?
- artifactory - 保存 Artifactory 存储的最佳方法
- javascript - 如何知道类的实例是否来自单例?
- python - 如何从 pyspark 数据帧中保存文件,以便稍后访问以将其上传到 S3?