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)) |
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)) |
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)) |
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)) |
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 #4: 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 |
[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.
Please Login to comment...