Open In App

Python – Convert Delimiter separated list to Number

Improve
Improve
Like Article
Like
Save
Share
Report

Given a String with delimiter separated numbers, concatenate to form integer after removing delimiter.

Input : test_str = “1@6@7@8”, delim = ‘@’ 
Output : 1678 
Explanation : Joined elements after removing delim “@”
Input : test_str = “1!6!7!8”, delim = ‘!’ 
Output : 1678 
Explanation : Joined elements after removing delim “!” 
 

Method #1 : Using loop + split() + int()

This is one of the ways in which this task can be performed. In this, we split the string on delimiter and then run a loop to concat, at end result is converted to int().

Python3




# Python3 code to demonstrate working of
# Convert Delimiter separated list to Number
# Using loop + split() + join()
 
# initializing string
test_str = "1@6@7@8@5@8@9"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Delimiter
delim = "@"
 
# splitting to get list of string numbers
temp = test_str.split(delim)
res = ''
for ele in temp:
    res = res + ele
 
# converting result into integer
res = int(res)
 
# printing result
print("Constructed integer : " + str(res))


Output

The original string is : 1@6@7@8@5@8@9
Constructed integer : 1678589

Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n).

Method #2 : Using join() + split() + int()

This is one another way in which this task can be performed. In this, we perform final concatenation using join() and int() to get final result.

Python3




# Python3 code to demonstrate working of
# Convert Delimiter separated list to Number
# Using join() + split() + int()
 
# initializing string
test_str = "1@6@7@8@5@8@9"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Delimiter
delim = "@"
 
# join used over splitted result
# final result casted using int()
res = int("".join(test_str.split(delim)))
 
# printing result
print("Constructed integer : " + str(res))


Output

The original string is : 1@6@7@8@5@8@9
Constructed integer : 1678589

Time complexity: O(n), where n is the length of the input string test_str. 
Auxiliary space: O(n), where n is the length of the input string.

Method #3: Using replace() method.

Replacing the delimiter in string with empty string

Python3




# Python3 code to demonstrate working of
# Convert Delimiter separated list to Number
 
 
# initializing string
test_str = "1@6@7@8@5@8@9"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Delimiter
delim = "@"
 
res = test_str.replace(delim,"")
 
# printing result
print("Constructed integer : " + str(res))


Output

The original string is : 1@6@7@8@5@8@9
Constructed integer : 1678589

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

Method #4 : Using for loop

Python3




# Python3 code to demonstrate working of
# Convert Delimiter separated list to Number
 
 
# initializing string
test_str = "1@6@7@8@5@8@9"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Delimiter
delim = "@"
res=""
for i in test_str:
    if(i!=delim):
        res+=i
# printing result
print("Constructed integer : " + str(res))


Output

The original string is : 1@6@7@8@5@8@9
Constructed integer : 1678589

Time Complexity:
The code has a linear time complexity, O(n), where n is the length of the input string test_str. The for loop iterates through each character in the input string, and the if statement performs a constant-time operation to check if the character is equal to the delimiter delim. Therefore, the overall time complexity of the program is O(n).

Auxiliary Space:
The program uses a string variable res to construct the resulting integer from the input string. The length of the resulting integer is the same as the length of the input string without the delimiter characters. Therefore, the auxiliary space used by the program is also O(n).

Method #5: Using map() and join() functions

In this method, we use the map() function to convert each string number obtained from split() function to an integer. We then join these integers using the join() function and finally convert the result to an integer using the int() function.

Python3




# Python3 code to demonstrate working of
# Convert Delimiter separated list to Number
# Using map() + join()
 
# initializing string
test_str = "1@6@7@8@5@8@9"
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Delimiter
delim = "@"
 
# using map() to convert each string number to integer
temp = list(map(int, test_str.split(delim)))
 
# joining the integers to form a single integer
res = int("".join(map(str, temp)))
 
# printing result
print("Constructed integer : " + str(res))


Output

The original string is : 1@6@7@8@5@8@9
Constructed integer : 1678589

Time complexity: O(n), where n is the length of the input string test_str. 
Auxiliary space: O(n), where n is the length of the input string test_str.

Method #6: Using list comprehension and join() function.

In this approach, use list comprehension to convert each string number to integer, and then use join() function to concatenate the integers into a single string. Finally, convert the string back to an integer using the int() function

Python3




test_str = "1@6@7@8@5@8@9"
delim = "@"
 
# Using list comprehension to convert each string number to integer
temp = [int(num) for num in test_str.split(delim)]
 
# Joining the integers to form a single integer
res = int("".join(str(num) for num in temp))
 
print("Constructed integer : " + str(res))


Output

Constructed integer : 1678589

Time complexity: O(n), where n is the length of the input string. 
Auxiliary space: O(n), where n is the length of the input string.

Method #7: Using reduce():

  1. Import the reduce() method from the functools module.
  2. Define a string test_str.
  3. Pass a lambda function to reduce() that takes two parameters: acc and x. acc is an accumulator variable that stores the concatenated characters encountered so far, and x is the current character being processed.
  4. Split the original string test_str using the delimiter @ and pass the resulting list of substrings as the second argument to reduce().
  5. The lambda function concatenates the current character x to the accumulator acc.
  6. Start with an initial value of an empty string ” for the accumulator.
  7. Store the result of reduce() in the variable res.
  8. Print the original string and the constructed integer.

Python3




from functools import reduce
 
test_str = "1@6@7@8@5@8@9"
 
# using reduce() to convert the
# delimiter-separated list to an integer
res = reduce(lambda acc, x: acc + x, test_str.split('@'), '')
 
# printing original string and result
print("The original string is : " + str(test_str))
 
print("Constructed integer : " + str(res))


Output

The original string is : 1@6@7@8@5@8@9
Constructed integer : 1678589

Time Complexity: O(n), where n is the length of the list of substrings obtained by splitting the original string. This is because the method applies the lambda function to each substring in the list exactly once.

Space Complexity: O(n), where n is the length of the list of substrings obtained by splitting the original string. This is because the method creates a string to store the concatenated characters, whose length can be at most n. Additionally, the lambda function only stores the current character being processed and the accumulator variable, which take constant space.



Last Updated : 22 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads