The most efficient way to remove non-numeric list entries

I am looking to “clear” the list by excluding any elements containing characters other than 0-9, and I wonder if there is a more efficient way than, for example,

import re
invalid = re.compile('[^0-9]')    
ls = ['1a', 'b3', '1']
cleaned = [i for i in ls if not invalid.search(i)]
print cleaned
>> ['1']

How am I going to work with large lists (5 thousand items) of long strings (15 characters).

+5
source share
2 answers

Is there something wrong with the string method isdigit?

>>> ls = ['1a', 'b3', '1']
>>> cleaned = [ x for x in ls if x.isdigit() ]
>>> cleaned
['1']
>>>
+11
source

You can use the isnumeric function. It checks if a string contains only numeric characters. This method is present only for Unicode objects. It will not work with integer or floating values.

myList = ['text', 'another text', '1', '2.980', '3']
output = [ a for a in myList if a.isnumeric() ]
print( output )      
# Output is : ['1', '3']

Link: https://www.tutorialspoint.com/python/string_isnumeric.htm

0

All Articles