Python | Convert Stream of numbers to list
Sometimes, we can be stuck with a problem in which we are given a stream of space separated numbers with a goal to convert them into a list of numbers. This type of problem can occur in common day-day programming or competitive programming while taking inputs. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using list() + split() The space separated numbers can be converted to list by using a simple split function that would convert the string to list of numbers and hence solve our problem.
Python3
test_str = "10 12 3 54 6 777 443"
print ( "The original string is : " + test_str)
res = list (test_str.split())
print ( "The list of stream of numbers : " + str (res))
|
Output
The original string is : 10 12 3 54 6 777 443
The list of stream of numbers : ['10', '12', '3', '54', '6', '777', '443']
Method #2 : Using map() + split() + list() Since the drawback of above method is that the conversion doesn’t change the datatype of the unit numbers, so if it’s desired to change the data type of number as well, it is suggested to additionally use map() to have list of strings as integers.
Python3
test_str = "10 12 3 54 6 777 443"
print ( "The original string is : " + test_str)
res = list ( map ( int , test_str.split()))
print ( "The list of stream of numbers : " + str (res))
|
Output
The original string is : 10 12 3 54 6 777 443
The list of stream of numbers : [10, 12, 3, 54, 6, 777, 443]
Method #3: Using re.findall() function
Using re.findall() function, we can find all digits in the string and then convert them to a list of integers
Python3
import re
test_str = "10 12 3 54 6 777 443"
print ( "The original string is : " + test_str)
res = re.findall(r '\d+' , test_str)
res = list ( map ( int , res))
print ( "The list of stream of numbers : " + str (res))
|
Output
The original string is : 10 12 3 54 6 777 443
The list of stream of numbers : [10, 12, 3, 54, 6, 777, 443]
The re.findall() function is from the re module, which is the python built-in regular expression module. The findall() function returns a list of all non-overlapping matches of the specified regular expression in the string. Here we are using regular expression \d+ which will match one or more digits. We are using findall() to find all occurrences of digits in the string. Then we are using map() function to convert the digits to integers. Finally, we are converting the map object to a list.
Time complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using List comprehension:
Python3
test_str = "10 12 3 54 6 777 443"
res = [ int (num) for num in test_str.split()]
print ( "The original string is : " + test_str)
print ( "The list of stream of numbers:" , res)
|
Output
The original string is : 10 12 3 54 6 777 443
The list of stream of numbers: [10, 12, 3, 54, 6, 777, 443]
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #5:Using enumeration
Algorithm:
- Initialize the stream of numbers as a string
- Split the string by space to get a list of numbers as strings
- Create an empty list to store the result
- Loop through each number string in the list
a. Convert the number string to integer using the int() function
b. Append the integer to the result list
- Print the original string and the result list
Python3
test_str = "10 12 3 54 6 777 443"
res = []
for idx, num in enumerate (test_str.split()):
res.append( int (num))
print ( "The original string is : " + test_str)
print ( "The list of stream of numbers:" , res)
|
Output
The original string is : 10 12 3 54 6 777 443
The list of stream of numbers: [10, 12, 3, 54, 6, 777, 443]
Time complexity: O(n), where n is the number of numbers in the input string. This is because we need to loop through each number string in the list and convert it to an integer.
Auxiliary Space: O(n), where n is the number of numbers in the input string. This is because we need to store the list of numbers as integers in memory.
Last Updated :
28 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...