Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to remove non-numeric list entries

Tags:

python

I'm looking to 'clean' a list by excluding any items which contain characters other than 0-9, and wondering if there's a more efficient way than e.g.

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']

As I'm going to be operating on large-ish lists (5k items) of long strings (15 chars).

like image 964
urschrei Avatar asked May 17 '11 11:05

urschrei


People also ask

How do I remove non-numeric values from a list in Python?

Use the re. sub() method to remove all non-numeric characters except for dot . from a string, e.g. result = re. sub(r'[^0-9.]

How do I remove all entries from a list in Python?

Remove all items: clear() You can remove all items from the list with clear() .


2 Answers

You can use isnumeric function. It checks whether the string consists of only numeric characters. This method is present only on unicode objects. It won't work with integer or float values

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

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

like image 82
Forhadul Islam Avatar answered Oct 20 '22 20:10

Forhadul Islam


Anything wrong with the string method isdigit ?

>>> ls = ['1a', 'b3', '1']
>>> cleaned = [ x for x in ls if x.isdigit() ]
>>> cleaned
['1']
>>>
like image 34
MattH Avatar answered Oct 20 '22 20:10

MattH