如何识别字符串列表中的十进制数,以便删除它们?理想情况下,在单个操作中,类似content = [x for x in content if not x.isdecimal()]
(可悲的是,isdecimal()和isnumeric()不在这里工作)
例如,如果content = ['55', 'line', '0.04', 'show', 'IR', '50.5', 'find', 'among', '0.06', 'also', 'detected', '0.05', 'ratio', 'fashion.sense', '123442b']
我希望输出为content = ['line', 'show', 'IR', 'find', 'among', 'also', 'detected', 'ratio', 'fashion.sense', '123442b']
答案 0 :(得分:3)
您应该使用正则表达式来测试字符串是否为小数:
import re
content = ['line', '0.04', 'show', 'IR', '50.5', 'find', 'among', '0.06', 'also', 'detected', '0.05', 'ratio', 'fashion.sense', '123442b']
regex = r'^[+-]{0,1}((\d*\.)|\d*)\d+$'
content = [x for x in content if re.match(regex, x) is None]
print(content)
# => ['line', 'show', 'IR', 'find', 'among', 'also', 'detected', 'ratio', 'fashion.sense', '123442b']
答案 1 :(得分:0)