我需要检查值(例如13或17.5)是否存在于字符串列表中定义的范围中,例如:
L = ["12-14", "15-16", "17-20"]
Run Code Online (Sandbox Code Playgroud)
例如,该范围是12要14包括,15以16等
我想要:
if "13.5" in L:
print("yes")
else:
print("no")
Run Code Online (Sandbox Code Playgroud)
预期产量: 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
Run Code Online (Sandbox Code Playgroud)