我有这根绳子 "Desirable: < 200 Borderline HIgh: 200 - 240 High: > 240" 其中我只想提取唯一的数字或十进制值。
"Desirable: < 200 Borderline HIgh: 200 - 240 High: > 240"
提取 Number,Decimal,- 我使用了这个正则表达式代码 r'[^0-9.-]+' 但它不会返回唯一值:
Number,Decimal,-
r'[^0-9.-]+'
import re check = "Desirable: < 200 Borderline HIgh: 200 - 240 High: > 240" re.sub(r'[^0-9.-]+', '',check)
输出: 200200-240240
200200-240240
期望输出: 200-240
200-240
请注意:能够提取很重要 Numbers, Decimals,- 从字符串。
Numbers, Decimals,-
您可以使用以下命令提取所有数字,包括十进制数字:
re.findall(r'-?\d+\.?\d*', check)
然后,您可以使用 set() 最后使用 "-".join
set()
"-".join
您想要的代码:
"-".join(set(re.findall(r'-?\d+\.?\d*', check)))
该代码的挑战之一是 set() 不能保持数字的顺序。如果订单对您很重要,可以使用 numpy.unique() 相反:
numpy.unique()
"-".join(np.unique(re.findall(r'-?\d+\.?\d*', check)))