I need to filter out only strings that contains only digits and/or a fix set of punctuation.
I've tried checking each character and then summing the Boolean conditions to check if it is equal to the len(str)
. Is there a more pythonic way to do this:
>>> import string
>>> x = ['12,523', '3.46', "this is not", "foo bar 42", "23fa"]
>>> [i for i in x if [True if j.isdigit() else False for j in i] ]
['12,523', '3.46', 'this is not', 'foo bar 42']
>>> [i for i in x if sum([True if j.isdigit() or j in string.punctuation else False for j in i]) == len(i)]
['12,523', '3.46']
float
etc. doesn't work because I also want to allow for commas as thousands separators"? – Marivelmariyax = ["chapter", "1.2.3.5"]
) – Ciborium