【发布时间】:2019-04-25 07:53:02
【问题描述】:
我有以下字符串列表 (list1),我想查找 str b 是否存在于 list1 中小数点前元素左侧部分的任何位置。
我尝试了下面的代码,但它找到了找到 str b 的所有元素。
list1= ['4.39', '5.25', '2.29', '3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16', '4.4']
b=str(1)
print([s for s in list1 if b in s])
它返回以下内容:
['3.16', '4.19', '1.5', '4.17', '2.18', '5.18', '4.18', '5.16']
但是,我只想得到1.5,因为这是字符串 b 与小数点前左侧部分匹配的唯一元素。记住元素是字符串格式的。有什么快速检查这个东西的方法吗?
【问题讨论】:
标签: python string list list-comprehension python-3.5