Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Extract numbers from list of strings

Python | Extract numbers from list of strings

Sometimes, we can data in many forms and we desire to perform both conversions and extractions of certain specific parts of a whole. One such issue can be extracting a number from a string and extending this, sometimes it can be more than just an element string but a list of it. Let’s discuss certain ways in which this can be solved.

Method #1 : Using list comprehension + split()
This particular problem can be solved using the list comprehension function to extend the logic to all the items and split function performs the task of splitting and fetching the target desired element. 

Python3




# Python3 code to demonstrate
# Extracting numbers from list of strings
# using list comprehension + split()
 
# initializing list
test_list = ['Rs. 24', 'Rs. 18', 'Rs. 8', 'Rs. 21']
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + split()
# Extracting numbers from list of strings
res = [int(sub.split('.')[1]) for sub in test_list]
 
# print result
print("The list after Extracting numbers : " + str(res))


Output : 

The original list : ['Rs. 24', 'Rs. 18', 'Rs. 8', 'Rs. 21']
The list after Extracting numbers : [24, 18, 8, 21]

Time Complexity: O(n), where n is the length of the given test_list
Auxiliary Space: O(n)

Method #2 : Using join() + isnumeric() + list comprehension + map()
This method is preferred in the instances in which it is not predefined that the numbers will be ordered in a particular way i.e, this method gives the flexibility of getting the number from whichever position possible. 

Python3




# Python3 code to demonstrate
# Extracting numbers from list of strings
# using join() + isnumeric() + list comprehension + map()
 
# initializing list
test_list = ['Rs. 24', 'Rs. 18', 'Rs. 8', 'Rs. 21']
 
# printing original list
print("The original list : " + str(test_list))
 
# using join() + isnumeric() + list comprehension + map()
# Extracting numbers from list of strings
res = list(map(lambda sub:int(''.join(
      [ele for ele in sub if ele.isnumeric()])), test_list))
 
# print result
print("The list after Extracting numbers : " + str(res))


Output : 

The original list : ['Rs. 24', 'Rs. 18', 'Rs. 8', 'Rs. 21']
The list after Extracting numbers : [24, 18, 8, 21]

Time Complexity: O(n), where n is the length of the input list test_list.
Auxiliary Space: O(n), because the result list res has the same length as test_list

Method #3 : Using replace() and int() methods

Python3




# Python3 code to demonstrate
# Extracting numbers from list of strings
 
 
# initializing list
test_list = ['Rs. 24', 'Rs. 18', 'Rs. 8', 'Rs. 21']
 
# printing original list
print("The original list : " + str(test_list))
 
 
# Extracting numbers from list of strings
res=[]
for i in test_list:
    i=i.replace("Rs.","")
    res.append(int(i))
 
# print result
print("The list after Extracting numbers : " + str(res))


Output

The original list : ['Rs. 24', 'Rs. 18', 'Rs. 8', 'Rs. 21']
The list after Extracting numbers : [24, 18, 8, 21]

Time Complexity: O(n), where n is the number of elements in the list ‘test_list’. The code iterates through the entire list once and extracts the numbers from each string.
Auxiliary Space: O(n), where n is the number of elements in the list ‘test_list’. This is because a new list ‘res’ is created to store the extracted numbers, which takes O(n) space.

Method #4 : Using index() and int() methods

Python3




# Python3 code to demonstrate
# Extracting numbers from list of strings
 
 
# initializing list
test_list = ['Rs. 24', 'Rs. 18', 'Rs. 8', 'Rs. 21']
 
# printing original list
print("The original list : " + str(test_list))
 
 
# Extracting numbers from list of strings
res=[]
for i in test_list:
    j=i.index(".")
    res.append(int(i[j+1:]))
 
# print result
print("The list after Extracting numbers : " + str(res))


Output

The original list : ['Rs. 24', 'Rs. 18', 'Rs. 8', 'Rs. 21']
The list after Extracting numbers : [24, 18, 8, 21]

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

Method #5: Using re

This code defines a function extract_numbers that takes a list of strings as input and returns a list of the integers that are present in those strings.
The first step is to compile a regular expression pattern using the re module. The pattern \d+ will match any sequence of one or more digits.
Next, the code uses a list comprehension to apply the findall function to each string in the input list. The findall function returns a list of all the substrings in the string that match the regular expression pattern.

Finally, another list comprehension is used to convert the extracted numbers from strings to integers, and the resulting list is returned.

Python3




import re
 
def extract_numbers(lst):
    """
    Extracts numbers from a list of strings using regular expressions.
    """
    # Compile a regular expression pattern to match digits
    pattern = re.compile(r'\d+')
     
    # Use the pattern to extract all digits from each string in the list
    extracted_numbers = [pattern.findall(s) for s in lst]
     
    # Convert the extracted numbers from strings to integers
    return [int(x) for sublist in extracted_numbers for x in sublist]
 
# Example usage
test_list = ['Rs. 24', 'Rs. 18', 'Rs. 8', 'Rs. 21']
print(extract_numbers(test_list))
#This code is contributed by Edula Vinay Kumar Reddy


Output

[24, 18, 8, 21]

Time Complexity: O(n), where n is the total number of characters in all the strings in the list. 
Auxiliary Space: O(n).

This method is more flexible than the other approaches because it allows you to extract any sequence of digits from the strings, not just the ones that appear at a fixed position or are separated by a specific character. It also allows you to handle strings that contain multiple instances of digits.

Method 5: Using map() and filter():

Use the map() and filter() functions in Python to extract the numbers from the strings. First, we can use map() to apply the int() function to each element of the list, which will convert each string to an integer (if it can be converted). Then, we can use filter() to remove any None values that result from trying to convert a string that doesn’t contain a number.

Python3




test_list = ['Rs. 24', 'Rs. 18', 'Rs. 8', 'Rs. 21']
 
# define a function to extract the number from a string
def extract_num(string):
    try:
        return int(string.split('.')[1])
    except IndexError:
        return None
 
# use map() to apply the extract_num() function to each element of the list
nums = map(extract_num, test_list)
 
# use filter() to remove any None values from the list
res = list(filter(lambda x: x is not None, nums))
 
print("The list after Extracting numbers : ", res)


Output

The list after Extracting numbers :  [24, 18, 8, 21]

Time complexity: O(n), where n is the number of elements in the input list.
Auxiliary space: O(n), as it involves creating a new list of numbers using map() and filter().

Method #7: Using regular expression and list comprehension:

This method uses regular expression to match numbers after a dot in each string and extracts them using list comprehension. 

Python3




import re
 
test_list = ['Rs. 24', 'Rs. 18', 'Rs. 8', 'Rs. 21']
 
# define a regular expression pattern to match numbers after a dot
pattern = r'\.\s*(\d+)'
 
# use list comprehension to extract numbers using regular expression
res = [int(re.findall(pattern, string)[0]) for string in test_list if re.findall(pattern, string)]
 
print("The list after Extracting numbers: ", res)


Output

The list after Extracting numbers:  [24, 18, 8, 21]

Time complexity: O(n) where n is the number of elements in the list
Auxiliary space: O(n) for the resulting list.

Using numpy:

Algorithm:

Initialize a list of strings test_list.
Print the original list test_list.
Use numpy.char.replace() method to remove ‘Rs. ‘ from each string in the list test_list.
Use numpy.char.strip() method to remove leading and trailing whitespaces from each string in the list test_list.
Convert the resulting list of strings to numpy array and use numpy.astype() method to convert the array elements to integers.
Store the resulting array in a variable named res.
Print the final list of integers res.

Python3




import numpy as np
 
# initializing list
test_list = ['Rs. 24', 'Rs. 18', 'Rs. 8', 'Rs. 21']
 
# printing original list
print("The original list : " + str(test_list))
 
# Using numpy.char.replace() and numpy.char.strip() methods to extract numbers from list of strings
# converting dtype of the array to int
res = np.char.replace(test_list, 'Rs. ', '').astype(int)
 
# printing result
print("The list after Extracting numbers : " + str(res))


Output:
The original list : [‘Rs. 24’, ‘Rs. 18’, ‘Rs. 8’, ‘Rs. 21’]
The list after Extracting numbers : [24 18  8 21]

The time complexity of the given algorithm is O(n), where n is the length of the input list.

The space complexity of the algorithm is O(n), where n is the length of the input list. This is because the algorithm creates a new array of integers with the same length as the input list.

RELATED ARTICLES

Most Popular

Recent Comments