Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove non numeric values from list python

Tags:

python

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.

like image 339
Pie Avatar asked Jan 24 '23 14:01

Pie


2 Answers

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']
like image 60
Sash Sinha Avatar answered May 22 '23 14:05

Sash Sinha


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:

  • string.split(delim) will tokenize a string by the given delimiter.
  • int(string) will parse a string into an integer
  • float(string) will parse a string into a floating point number.

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

like image 38
T. N. Williams Avatar answered May 22 '23 13:05

T. N. Williams