Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Extract numbers from string

Python | Extract numbers from string

Many times, while working with strings we come across this issue in which we need to get all the numeric occurrences. This type of problem generally occurs in competitive programming and also in web development. Let’s discuss certain ways in which this problem can be solved in Python

Input: "There are 2 apples for 4 persons"
Output: [2, 4]
Explanation: 2 and 4 are the only number present in the input string.

Extract Numbers from a String in Python

Below are the methods that we will cover in this article:

Extract numbers from string using list comprehension and isdigit() method

This problem can be solved by using the split function to convert string to list and then the list comprehension which can help us iterate through the list and isdigit function helps to get the digit out of a string. 

Python3




test_string = "There are 2 apples for 4 persons"
 
# printing original string
print("The original string : " + test_string)
 
# using List comprehension + isdigit() +split()
# getting numbers from string
res = [int(i) for i in test_string.split() if i.isdigit()]
 
# print result
print("The numbers list is :" + str(res))


Output

The original string : There are 2 apples for 4 persons
The numbers list is :[2, 4]

Time Complexity: O(n), where n is the number of elements in the input string.
Auxiliary Space: O(n), where n is the number of numbers in the input string.

Extract Digit from string using re.findall() method

This particular problem can also be solved using Python regex, we can use the findall function to check for the numeric occurrences using a matching regex string. 

Python3




import re
 
# initializing string
test_string = "There are 2 apples for 4 persons"
 
# printing original string
print("The original string : " + test_string)
 
# getting numbers from string
temp = re.findall(r'\d+', test_string)
res = list(map(int, temp))
 
# print result
print("The numbers list is : " + str(res))


Output

The original string : There are 2 apples for 4 persons
The numbers list is : [2, 4]

Extract Interger from string using isnumeric() method

In Python, we have isnumeric function which can tell the user whether a particular element is a number or not so by this method we can also extract the number from a string.

Python3




test_string = "There are 2 apples for 4 persons"
 
# printing original string
print("The original string : " + test_string)
 
 
# getting numbers from string
res = []
x=test_string.split()
for i in x:
    if i.isnumeric():
        res.append(int(i))
 
# print result
print("The numbers list is : " + str(res))


Output

The original string : There are 2 apples for 4 persons
The numbers list is : [2, 4]

Time Complexity : O(N)
Auxiliary Space : O(N)

Extract Digit from string using Filter() function

First, we define the input string then print the original string and split the input string into a list of words using the split() method. Use the filter() function to filter out non-numeric elements from the list by applying the lambda function x.isdigit() to each elementConvert the remaining elements in the filtered list to integers using a list comprehension

Print the resulting list of integers

Python3




test_string = "There are 2 apples for 4 persons"
print("The original string : " + test_string)
 
# use the split() method to split
# use the filter() function to filter out non-numeric elements from the list
res = list(filter(lambda x: x.isdigit(), test_string.split()))
 
# use a list comprehension to convert the remaining elements to integers
res = [int(s) for s in res]
 
# print the resulting list of integers
print("The numbers list is : " + str(res))


Output

The original string : There are 2 apples for 4 persons
The numbers list is : [2, 4]

Time complexity: O(n), where n is the length of the input string. The split() method takes O(n) time to split the input string into a list of words, and the filter() function takes O(n) time to iterate over each element in the list and apply the lambda function. The list comprehension takes O(k) time, where k is the number of elements in the filtered list that are digits, and this is typically much smaller than n. Therefore, the overall time complexity is O(n).

Auxiliary space complexity: O(n), as the split() method creates a list of words that has the same length as the input string, and the filter() function creates a filtered list that can be up to the same length as the input list. The list comprehension creates a new list of integers that is typically much smaller than the input list, but the space complexity is still O(n) in the worst case. Therefore, the overall auxiliary space complexity is O(n)

Extract Interger from string using a loop and isdigit() method

Use a loop to iterate over each character in the string and check if it is a digit using the isdigit() method. If it is a digit, append it to a list.

Python3




test_string = "There are 2 apples for 4 persons"
 
numbers = []
for char in test_string:
    if char.isdigit():
        numbers.append(int(char))
 
print("The numbers list is:", numbers)


Output

The numbers list is: [2, 4]

Time complexity: O(n), where n is the length of the string.
Auxiliary space: O(k), where k is the number of digits in the string.

Extract Numbers from string using str.translate() with str.maketrans() 

Define the input string then Initialize a translation table to remove non-numeric characters using str.maketrans(). Use str.translate() with the translation table to remove non-numeric characters from the string and store the result in a new string called numeric_string. Use str.split() to split the numeric_string into a list of words and store the result in a new list called words. Initialize an empty list called numbers to store the resulting integers and then iterate over each word in the list of words. Check if the word is a numeric string using str.isdigit().If the word is a numeric string, convert it to an integer using int() and append it to the list of numbers.

Print the resulting list of integers.

Below is the implementation of the above approach:

Python3




# Define the input string
test_string = "There are 2 apples for 4 persons"
# Print the original string
print("The original string : " + test_string)
# Initialize a translation table to remove non-numeric characters
translation_table = str.maketrans('', '', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
# Use str.translate() with the translation table to remove non-numeric characters
numeric_string = test_string.translate(translation_table)
# Use str.split() to split the string into a list of word
words = numeric_string.split()
numbers = [int(i) for i in words]
print("The numbers list is : " + str(numbers))


Output

The original string : There are 2 apples for 4 persons
The numbers list is : [2, 4]





Time complexity: O(n), where n is the length of the input string. The str.translate() method and str.split() method take O(n) time, and iterating over each word in the list of words takes O(k) time, where k is the number of words in the list that are numeric strings.
Auxiliary Space: O(n), as we create a new string and a new list of words that each have the same length as the input string, and we create a new list of integers that has a maximum length of k, where k is the number of words in the list that are numeric strings.

Extract Numbers from string using numpy module

Initialize the string test_string then split the string into a list of words using the split method and create a numpy array x from the resulting list. Use np.char.isnumeric to create a boolean mask indicating which elements of x are numeric. Use this boolean mask to index x and extract only the numeric elements. Convert the resulting array of strings to an array of integers using astype.

Print the resulting array of integers.

Python3




import numpy as np
 
# initializing string
test_string = "There are 2 apples for 4 persons"
 
# printing original string
print("The original string : " + test_string)
 
# getting numbers from string using numpy
x = np.array(test_string.split())
res = x[np.char.isnumeric(x)].astype(int)
 
# print result
print("The numbers list is : " + str(res))
#This code is contributed by Vinay Pinjala.


Output:

The original string : There are 2 apples for 4 persons
The numbers list is : [2 4]

Time complexity:  O(n), where n is the length of the original string test_string. This is because the split method takes O(n) time to split the string into a list of words, and the np.char.isnumeric method takes O(n) time to create the boolean mask. The remaining operations take constant time.

Auxiliary Space: O(n), where n is the length of the original string test_string. This is because we create a numpy array x to store the words of the string, which takes O(n) space. The space used by the resulting numpy array of integers is also O(n), since it contains all the numeric elements of the string.

RELATED ARTICLES

Most Popular

Recent Comments