How do you remove non digits from a string in python?

In Python 2.*, by far the fastest approach is the .translate method:

>>> x='aaa12333bb445bb54b5b52'
>>> import string
>>> all=string.maketrans['','']
>>> nodigs=all.translate[all, string.digits]
>>> x.translate[all, nodigs]
'1233344554552'
>>> 

string.maketrans makes a translation table [a string of length 256] which in this case is the same as ''.join[chr[x] for x in range[256]] [just faster to make;-]. .translate applies the translation table [which here is irrelevant since all essentially means identity] AND deletes characters present in the second argument -- the key part.

.translate works very differently on Unicode strings [and strings in Python 3 -- I do wish questions specified which major-release of Python is of interest!] -- not quite this simple, not quite this fast, though still quite usable.

Back to 2.*, the performance difference is impressive...:

$ python -mtimeit -s'import string; all=string.maketrans["", ""]; nodig=all.translate[all, string.digits]; x="aaa12333bb445bb54b5b52"' 'x.translate[all, nodig]'
1000000 loops, best of 3: 1.04 usec per loop
$ python -mtimeit -s'import re;  x="aaa12333bb445bb54b5b52"' 're.sub[r"\D", "", x]'
100000 loops, best of 3: 7.9 usec per loop

Speeding things up by 7-8 times is hardly peanuts, so the translate method is well worth knowing and using. The other popular non-RE approach...:

$ python -mtimeit -s'x="aaa12333bb445bb54b5b52"' '"".join[i for i in x if i.isdigit[]]'
100000 loops, best of 3: 11.5 usec per loop

is 50% slower than RE, so the .translate approach beats it by over an order of magnitude.

In Python 3, or for Unicode, you need to pass .translate a mapping [with ordinals, not characters directly, as keys] that returns None for what you want to delete. Here's a convenient way to express this for deletion of "everything but" a few characters:

import string

class Del:
  def __init__[self, keep=string.digits]:
    self.comp = dict[[ord[c],c] for c in keep]
  def __getitem__[self, k]:
    return self.comp.get[k]

DD = Del[]

x='aaa12333bb445bb54b5b52'
x.translate[DD]

also emits '1233344554552'. However, putting this in xx.py we have...:

$ python3.1 -mtimeit -s'import re;  x="aaa12333bb445bb54b5b52"' 're.sub[r"\D", "", x]'
100000 loops, best of 3: 8.43 usec per loop
$ python3.1 -mtimeit -s'import xx; x="aaa12333bb445bb54b5b52"' 'x.translate[xx.DD]'
10000 loops, best of 3: 24.3 usec per loop

...which shows the performance advantage disappears, for this kind of "deletion" tasks, and becomes a performance decrease.

Remove all non-numeric characters from a String in Python #

Use the re.sub[] method to remove all non-numeric characters from a string, e.g. result = re.sub[r'[^0-9]', '', my_str]. The re.sub[] method will remove all non-numeric characters from the string by replacing them with empty strings.

Copied!

import re my_str = 'a1s2d3f4g5' result = re.sub[r'[^0-9]', '', my_str] print[result] # 👉️ '12345'

If you're looking to avoid using regular expressions, scroll down to the next subheading.

We used the re.sub[] method to remove all non-numeric characters from a string.

The re.sub method returns a new string that is obtained by replacing the occurrences of the pattern with the provided replacement.

Copied!

import re my_str = '1apple, 2apple, 3banana' result = re.sub[r'[^0-9]', '', my_str] print[result] # 👉️ 123

If the pattern isn't found, the string is returned as is.

The first argument we passed to the re.sub[] method is a regular expression.

The square brackets [] are used to indicate a set of characters.

If the first character of the set is a caret ^, all characters that are not in the set will be matched.

In other words, our set matches any character that is not a digit in the range 0-9.

The second argument we passed to the re.sub[] method is the replacement for each match.

Copied!

import re my_str = 'a1s2d3f4g5' result = re.sub[r'[^0-9]', '', my_str] print[result] # 👉️ '12345'

We want to remove all non-numeric characters, so we replace each with an empty string.

There is also a shorthand for the [^0-9] character set.

Copied!

import re my_str = 'a1s2d3f4g5' result = re.sub[r'\D', '', my_str] print[result] # 👉️ '12345'

The \D special character matches any character that is not a digit. It is very similar to the [^0-9] character set but includes more digit characters.

Remove all non-numeric characters from a String using join[] #

To remove all non-numeric characters from a string:

  1. Use a generator expression to iterate over the string.
  2. Use the str.isdigit[] character to check if each character is a digit.
  3. Use the str.join[] method to join the digits into a string.

Copied!

my_str = 'a1s2d3f4g5' result = ''.join[char for char in my_str if char.isdigit[]] print[result] # 👉️ '12345'

We used a generator expression to iterate over the string.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the str.isdigit[] method to check if the current character is a digit and return the result.

The generator object only contains the digits from the string.

Copied!

my_str = 'a1s2d3f4g5' # 👇️ ['1', '2', '3', '4', '5'] print[list[char for char in my_str if char.isdigit[]]]

The last step is to join the digits into a string.

Copied!

my_str = 'a1s2d3f4g5' result = ''.join[char for char in my_str if char.isdigit[]] print[result] # 👉️ '12345'

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

For our purposes, we called the join[] method on an empty string to join the digits without a separator.

How do you remove non

sub[] method to remove all non-numeric characters from a string, e.g. result = re. sub[r'[^0-9]', '', my_str] . The re. sub[] method will remove all non-numeric characters from the string by replacing them with empty strings.

How do I remove non digits from a string?

In order to remove all non-numeric characters from a string, replace[] function is used. replace[] Function: This function searches a string for a specific value, or a RegExp, and returns a new string where the replacement is done.

How do you remove a non number from a list in Python?

Use the re. sub[] method to remove all non-numeric characters except for dot . from a string, e.g. result = re. sub[r'[^0-9.]

How do I remove digits from a string in Python?

Remove Numbers From String in Python.
Remove Numbers From the String Using string.join[] Method in Python..
Remove Numbers From the String in Python Using the string.translate[] Method..
Remove Numbers From the String in Python Using the re.sub[] Method..

Chủ Đề