4

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.

Pie
  • 55
  • 4
  • Does this answer your question? [How do I convert a currency string to a floating point number in Python?](https://stackoverflow.com/questions/8421922/how-do-i-convert-a-currency-string-to-a-floating-point-number-in-python) you can replace `Decimal` with a simple `int` if you're never dealing with cents... – Tomerikoo Nov 17 '20 at 19:32
  • 2
    "I've found various samples of code but nothing is working for me." -- well, pick one that you've tried that you're most comfortable with and post your attempt and how your result differs from what you had hoped for. –  Nov 17 '20 at 19:32
  • Why don't you use regex if the string has the fixed format. In your case you can find the price using this re. `'\$([0-9,]*).*'`. Maybe you will get 1,500 as the group result. After getting this you can easily parse the number using `locale.atoi`. –  Nov 17 '20 at 19:38

2 Answers2

8

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']
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
1
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