简体   繁体   中英

Remove non numeric values from list 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.

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. in a similar fashion along with str. 以与str. 类似的方式str. :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM