我需要查看一行是否包含两个数字,和如果第一个小于0.5:
if re.search('((?:\d|[.\-\+]\d|[\+\-]\.\d)[\d\.\-\+e]*)[^\d\.\-\+]+((?:\d|[.\-\+]\d|[\-\+]\.\d)[\d\.\-\+e]*)',foil[ifrom],re.IGNORECASE) and float(re.group(1))<0.5:
#above is wrong: no such thing as re.group(1)...
elif re.search('((?:\d|[.\-\+]\d|[\+\-]\.\d)[\d\.\-\+e]*)[^\d\.\-\+]+((?:\d|[.\-\+]\d|[\-\+]\.\d)[\d\.\-\+e]*)',foil[midsep+1],re.IGNORECASE) and float(re.group(1))>0.5:
#also wrong
正确的方法是什么?甚至可以在同一个“if”语句中访问搜索结果吗
在Python 3.8+中使用walrus operator:
匹配数据对象被分配给
m
,并且m.group(1)
访问在相同if
条件下可用的组1值Python控制台中的快速测试:
相关问题 更多 >
编程相关推荐