Monday, November 18, 2024
Google search engine
HomeLanguagesPython | Convert Stream of numbers to list

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




# Python3 code to demonstrate working of
# Convert Stream of numbers to list
# Using list() + split()
 
# initializing string
test_str = "10 12 3 54 6 777 443"
 
# printing original string
print("The original string is : " + test_str)
 
# Using list() + split()
# Convert Stream of numbers to list
res = list(test_str.split())
 
# printing result
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




# Python3 code to demonstrate working of
# Convert Stream of numbers to list
# Using map() + split() + list()
 
# initializing string
test_str = "10 12 3 54 6 777 443"
 
# printing original string
print("The original string is : " + test_str)
 
# Using map() + split() + list()
# Convert Stream of numbers to list
res = list(map(int, test_str.split()))
 
# printing result
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
 
# Initializing the string containing the numbers
test_str = "10 12 3 54 6 777 443"
 
# Printing the original string
print("The original string is : " + test_str)
 
# Using re.findall() function to find all digits in the string
res = re.findall(r'\d+', test_str)
 
# Using map() function to convert the digits to integers
res = list(map(int, res))
 
# Printing the result
print("The list of stream of numbers : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


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




# Initialize the stream of numbers as a string
test_str = "10 12 3 54 6 777 443"
# Use a list comprehension to convert the string to a list of integers
res = [int(num) for num in test_str.split()]
# Printing the original string
print("The original string is : " + test_str)
# Print the result
print("The list of stream of numbers:", res)
#This code is contributed by Jyothi pinjala.


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:

  1. Initialize the stream of numbers as a string
  2. Split the string by space to get a list of numbers as strings
  3. Create an empty list to store the result
  4. 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
  5. Print the original string and the result list

Python3




#Initialize the stream of numbers as a string
test_str = "10 12 3 54 6 777 443"
 
#Use a list comprehension to convert the string to a list of integers
res = []
for idx, num in enumerate(test_str.split()):
    res.append(int(num))
 
#Printing the original string
print("The original string is : " + test_str)
 
#Print the result
print("The list of stream of numbers:", res)
#This code is contributed by Vinay Pinjala.


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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments