Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Frequency of numbers in String

Python | Frequency of numbers in String

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




# Python3 code to demonstrate working of
# Frequency of numbers in String
# Using re.findall() + len()
import re
 
# initializing string
test_str = "Lazyroar4feeks is No. 1 4 Lazyroar"
 
# printing original string
print("The original string is : " + test_str)
 
# Frequency of numbers in String
# Using re.findall() + len()
res = len(re.findall(r'\d+', test_str))
 
# printing result
print("Count of numerics in string : " + str(res))


Output : 

The original string is : Lazyroar4feeks is No. 1 4 Lazyroar
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




# Python3 code to demonstrate working of
# Frequency of numbers in String
# Using re.findall() + sum()
import re
 
# initializing string
test_str = "Lazyroar4feeks is No. 1 4 Lazyroar"
 
# printing original string
print("The original string is : " + test_str)
 
# Frequency of numbers in String
# Using re.findall() + sum()
res = sum(1 for _ in re.finditer(r'\d+', test_str))
 
# printing result
print("Count of numerics in string : " + str(res))


Output : 

The original string is : Lazyroar4feeks is No. 1 4 Lazyroar
Count of numerics in string : 3

Method #3 : Using isdigit() method

Python3




# Python3 code to demonstrate working of
# Frequency of numbers in String
 
# initializing string
test_str = "Lazyroar4feeks is No. 1 4 Lazyroar"
 
# printing original string
print("The original string is : " + test_str)
 
# Frequency of numbers in String
res=0
for i in test_str:
    if(i.isdigit()):
        res+=1
# printing result
print("Count of numerics in string : " + str(res))


Output

The original string is : Lazyroar4feeks is No. 1 4 Lazyroar
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




# Python3 code to demonstrate working of
# Frequency of numbers in String
 
# initializing string
test_str = "Lazyroar4feeks is No. 1 4 Lazyroar"
 
# printing original string
print("The original string is : " + test_str)
 
# Frequency of numbers in String
res=0
digits="0123456789"
for i in test_str:
    if(i in digits):
        res+=1
# printing result
print("Count of numerics in string : " + str(res))


Output

The original string is : Lazyroar4feeks is No. 1 4 Lazyroar
Count of numerics in string : 3

Method #5 : Using filter()+list()+len()+isdigit()+lambda functions

Python3




# Python3 code to demonstrate working of
# Frequency of numbers in String
 
# initializing string
test_str = "Lazyroar4feeks is No. 1 4 Lazyroar"
 
# printing original string
print("The original string is : " + test_str)
 
# Frequency of numbers in String
res = len(list(filter(lambda x: x.isdigit(), test_str)))
 
# printing result
print("Count of numerics in string : " + str(res))


Output

The original string is : Lazyroar4feeks is No. 1 4 Lazyroar
Count of numerics in string : 3

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #6 : Using map

Python3




# Python3 code to demonstrate working of
# Frequency of numbers in String
  
# initializing string
test_str = "Lazyroar4feeks is No. 1 4 Lazyroar"
  
# printing original string
print("The original string is : " + test_str)
  
# Frequency of numbers in String
res = sum(map(str.isdigit, test_str))
  
# printing result
print("Count of numerics in string : " + str(res))


Output

The original string is : Lazyroar4feeks is No. 1 4 Lazyroar
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.

RELATED ARTICLES

Most Popular

Recent Comments