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
test_list = [ 'Rs. 24' , 'Rs. 18' , 'Rs. 8' , 'Rs. 21' ]
print ("The original list : " + str (test_list))
res = [ int (sub.split( '.' )[ 1 ]) for sub in test_list]
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
test_list = [ 'Rs. 24' , 'Rs. 18' , 'Rs. 8' , 'Rs. 21' ]
print ("The original list : " + str (test_list))
res = list ( map ( lambda sub: int (''.join(
[ele for ele in sub if ele.isnumeric()])), test_list))
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
test_list = [ 'Rs. 24' , 'Rs. 18' , 'Rs. 8' , 'Rs. 21' ]
print ( "The original list : " + str (test_list))
res = []
for i in test_list:
i = i.replace( "Rs." ,"")
res.append( int (i))
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
test_list = [ 'Rs. 24' , 'Rs. 18' , 'Rs. 8' , 'Rs. 21' ]
print ( "The original list : " + str (test_list))
res = []
for i in test_list:
j = i.index( "." )
res.append( int (i[j + 1 :]))
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):
pattern = re. compile (r '\d+' )
extracted_numbers = [pattern.findall(s) for s in lst]
return [ int (x) for sublist in extracted_numbers for x in sublist]
test_list = [ 'Rs. 24' , 'Rs. 18' , 'Rs. 8' , 'Rs. 21' ]
print (extract_numbers(test_list))
|
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' ]
def extract_num(string):
try :
return int (string.split( '.' )[ 1 ])
except IndexError:
return None
nums = map (extract_num, test_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' ]
pattern = r '\.\s*(\d+)'
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
test_list = [ 'Rs. 24' , 'Rs. 18' , 'Rs. 8' , 'Rs. 21' ]
print ( "The original list : " + str (test_list))
res = np.char.replace(test_list, 'Rs. ' , '').astype( int )
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
05 May, 2023
Like Article
Save Article