Python | Frequency of numbers in String
Last Updated :
17 Apr, 2023
Sometimes, while working with Strings, we can have a problem in which we need to check how many of numerics are present in strings. This is a common problem and have application across many domains like day-day programming and data science. Lets discuss certain ways in which this task can be performed.
Method #1 : Using re.findall() + len() The combination of above functions can be used to perform this task. In this, we check for all numbers and put in list using findall() and the count is extracted using len().
Python3
import re
test_str = "geeks4feeks is No. 1 4 geeks"
print ( "The original string is : " + test_str)
res = len (re.findall(r '\d+' , test_str))
print ( "Count of numerics in string : " + str (res))
|
Output :
The original string is : geeks4feeks is No. 1 4 geeks
Count of numerics in string : 3
Time complexity: The time complexity of re.findall() function is O(n), where n is the length of the input string.
Auxiliary space: The auxiliary space used by re.findall() function is O(n), where n is the length of the input string.
Method #2 : Using sum() + findall() The combination of above functions can also be used to solve this problem. In this, we cumulate the sum using sum(). The task of findall() is to find all the numerics.
Python3
import re
test_str = "geeks4feeks is No. 1 4 geeks"
print ( "The original string is : " + test_str)
res = sum ( 1 for _ in re.finditer(r '\d+' , test_str))
print ( "Count of numerics in string : " + str (res))
|
Output :
The original string is : geeks4feeks is No. 1 4 geeks
Count of numerics in string : 3
Method #3 : Using isdigit() method
Python3
test_str = "geeks4feeks is No. 1 4 geeks"
print ( "The original string is : " + test_str)
res = 0
for i in test_str:
if (i.isdigit()):
res + = 1
print ( "Count of numerics in string : " + str (res))
|
Output
The original string is : geeks4feeks is No. 1 4 geeks
Count of numerics in string : 3
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Without using any builtin methods
Python3
test_str = "geeks4feeks is No. 1 4 geeks"
print ( "The original string is : " + test_str)
res = 0
digits = "0123456789"
for i in test_str:
if (i in digits):
res + = 1
print ( "Count of numerics in string : " + str (res))
|
Output
The original string is : geeks4feeks is No. 1 4 geeks
Count of numerics in string : 3
Method #5 : Using filter()+list()+len()+isdigit()+lambda functions
Python3
test_str = "geeks4feeks is No. 1 4 geeks"
print ( "The original string is : " + test_str)
res = len ( list ( filter ( lambda x: x.isdigit(), test_str)))
print ( "Count of numerics in string : " + str (res))
|
Output
The original string is : geeks4feeks is No. 1 4 geeks
Count of numerics in string : 3
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6 : Using map
Python3
test_str = "geeks4feeks is No. 1 4 geeks"
print ( "The original string is : " + test_str)
res = sum ( map ( str .isdigit, test_str))
print ( "Count of numerics in string : " + str (res))
|
Output
The original string is : geeks4feeks is No. 1 4 geeks
Count of numerics in string : 3
Time Complexity: O(n)
Auxiliary Space: O(1)
Note: In this approach, we are using map() function to convert all the characters of the string into True or False based on whether it is a digit or not. Then using sum() we are counting the number of True values, which is equivalent to counting the number of digits in the string.
Please Login to comment...