Python - find numbers in a string

def get_digits(str1): c = "" for i in str1: if i.isdigit(): c += i return c 

Above is the code I used, and the problem is that it only returns the first digit of the lines. For this, I have to keep both for the loop and return statement. Does anyone know how to fix it?

Thanks.

+7
source share
6 answers

Your indentation is a bit rooted (indentation in Python is very important). It's better:

 def get_digits(str1): c = "" for i in str1: if i.isdigit(): c += i return c 

A shorter and faster solution using generator expressions :

 ''.join(c for c in my_string if c.isdigit()) 
+7
source

As others have said, you have a semantic problem in your indentation, but you don't need to write such a function for this, a more Putin way to do this:

 def get_digits(text): return filter(str.isdigit, text) 

In the interpreter:

 >>> filter(str.isdigit, "lol123") '123' 

Some recommendations

Always test things when people show β€œfaster” methods:

 from timeit import Timer def get_digits1(text): c = "" for i in text: if i.isdigit(): c += i return c def get_digits2(text): return filter(str.isdigit, text) def get_digits3(text): return ''.join(c for c in text if c.isdigit()) if __name__ == '__main__': count = 5000000 t = Timer("get_digits1('abcdef123456789ghijklmnopq123456789')", "from __main__ import get_digits1") print t.timeit(number=count) t = Timer("get_digits2('abcdef123456789ghijklmnopq123456789')", "from __main__ import get_digits2") print t.timeit(number=count) t = Timer("get_digits3('abcdef123456789ghijklmnopq123456789')", "from __main__ import get_digits3") print t.timeit(number=count) ~# python tit.py 19.990989106 # Your original solution 16.7035926379 # Mine solution 24.8638381019 # Accepted solution 
+17
source

because your return is inside the for loop, so it returns after the first true if condition and stops.

  def get_digits(str1): c = "" for i in str1: if i.isdigit(): c += i return c 
+3
source

Your code was almost fine, except that the return should have been moved to the level of your for -loop.

 def get_digits(str1): c = "" for i in str1: if i.isdigit(): c += i return c ## <--- moved to correct level 

So now:

 get_digits('this35ad77asd5') 

gives:

 '35775' 

Explanation:

Previously, your function returned only the first digit, because when it found that the if was executed, like return (forcing you to return from the function, which means that you did not continue to look through the line).

Space / indentation really matters in Python, as you can see (unlike many other languages).

+2
source

there is a problem with indentation, which is returned when it finds the first digit, as with the current indentation, it is intetreted as a statement inside the if it must be parallel to the for statement to be considered outside the for .

 def get_digits(str1): c = "" for i in str1: if i.isdigit(): c += i return c digits = get_digits("abd1m4m3m22mmmbb4") print(digits) 

In braces, the equivalent of your invalid code is:

 def get_digits(str1){ c = "" for i in str1 { if i.isdigit(){ c += i return c # Notice the error here } } } 

And when the code is adjusted to move the return statement to match for , the equivalent is:

 def get_digits(str1){ c = "" for i in str1 { if i.isdigit(){ c += i } } return c # Correct as required } 
+1
source

Of course, it only returns the first digit, you explicitly tell Python to return as soon as you have the digit.

Indent the return and it should work:

 def get_digits(str1): c = "" for i in str1: if i.isdigit(): c += i # Note the indentation here return c 
+1
source

All Articles