Reputation: 55
How to do I go about removing non numerics from a list such as;
['$1,500 / month ', '$2,200 / month ', '$1,200 / month ']
to result in the following;
['1500', '2200', '1200']
I've found various samples of code but nothing is working for me.
Upvotes: 4
Views: 2162
Reputation: 51
Tokenize so that you only have the number string.
Tokenize by the comma, call the result tokens.
Set output = 0
For each token in tokens:
multiply output by 1000
parse token, call the result newnum
add newnum to output
return output
Some functions to look into:
Edit: Oh, this is wrong - this would return the numeric value of each value in the list. Though, technically, you could just return string(output) for the string version
Upvotes: 1
Reputation: 22370
You could use a list comprehension with a regular expression to replace all non numeric characters:
>>> import re
>>> lst = ['$1,500 / month ', '$2,200 / month ', '$1,200 / month ']
>>> lst
['$1,500 / month ', '$2,200 / month ', '$1,200 / month ']
>>> new_lst = [re.sub("[^0-9]", "", s) for s in lst]
>>> new_lst
['1500', '2200', '1200']
Or alternatively, use str.isdigit
in a similar fashion along with str.join
:
>>> lst = ['$1,500 / month ', '$2,200 / month ', '$1,200 / month ']
>>> lst
['$1,500 / month ', '$2,200 / month ', '$1,200 / month ']
>>> new_lst = [''.join(ch for ch in s if ch.isdigit()) for s in lst]
>>> new_lst
['1500', '2200', '1200']
Upvotes: 7