1

How can I identify decimal numbers in a list of strings, in order to remove them? Ideally in a single operation, something like content = [x for x in content if not x.isdecimal()]

(Sadly, isdecimal() and isnumeric() don't work here)

For instance, if content = ['55', 'line', '0.04', 'show', 'IR', '50.5', 'find', 'among', '0.06', 'also', 'detected', '0.05', 'ratio', 'fashion.sense', '123442b'] I would like the output to be content = ['line', 'show', 'IR', 'find', 'among', 'also', 'detected', 'ratio', 'fashion.sense', '123442b']

Unstack
  • 551
  • 3
  • 7
  • 13

2 Answers2

3

You should use a regex to test if a string is a decimal:

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']
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
0

Just adding to Mr Geek answer, You should also check out the python's docs on Regex.

Jermaine
  • 770
  • 7
  • 9