Reputation: 26859
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).
Upvotes: 14
Views: 32474
Reputation: 83197
In case one wants to keep both integers and floats in the output:
def is_float(string):
try:
float(string)
return True
except:
return False
example_list = ['text', 'another text', '1', '2.980', '3']
output = [a for a in example_list if is_float(a)]
print(output)
# Output is: ['1', '2.980', '3']
Alternatively, one can use filter
, as eumiro suggested:
output = list(filter(is_float, example_list))
print(output)
# Output is: ['1', '2.980', '3']
Credit: is_float()
comes from this answer by Saullo G. P. Castro.
Upvotes: -1
Reputation: 1237
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
Upvotes: 2
Reputation: 38247
Anything wrong with the string method isdigit
?
>>> ls = ['1a', 'b3', '1']
>>> cleaned = [ x for x in ls if x.isdigit() ]
>>> cleaned
['1']
>>>
Upvotes: 18