Open In App

Python – Custom length tuples from String

Improve
Improve
Like Article
Like
Save
Share
Report

Given a String, extract tuple list, with each tuple being of custom length, delimited using comma.

Input  :  test_str = “6 6 7, 3 4, 2”

Output : [(6, 6, 7), (3, 4), (2, )]

Explanation :  The customs lengths being 3, 2, 1 have been converted to tuple list.

Input  :  test_str  = “7, 7, 4”

Output : [(7, ), (7, ), (4, )]

Explanation :  All elements are of length 1.

Method #1 : Using int() + tuple() + split() + list comprehension

The combination of above functions can be used to solve this problem. In this, we perform conversion of string characters using int() and tuple() and split() is used to split on delimiter.

Python3




# Python3 code to demonstrate working of
# Custom length tuples from String
# Using int() + tuple() + split() + list comprehension
 
# initializing string
test_str = '4 6 7, 1 2, 3, 4 6 8 8'
 
# printing original string
print("The original string is : " + str(test_str))
 
# split() used to split on delimiter and
# type casted to int followed by tuple casting
test_str = test_str.split(', ')
res = [tuple(int(ele) for ele in sub.split()) for sub in test_str]
 
# printing result
print("The constructed custom length tuples : " + str(res))


Output

The original string is : 4 6 7, 1 2, 3, 4 6 8 8
The constructed custom length tuples : [(4, 6, 7), (1, 2), (3, ), (4, 6, 8, 8)]

Time Complexity: O(n), where n is the elements of list
Auxiliary Space: O(n), where n is the size of list

Method #2 : Using map() + int + tuple() + list comprehension + split()

The combination of above functions provide yet another way in which this task can be performed. In this we perform task using similar method as above just difference being using map() to extend logic of integer conversion to elements.

Python3




# Python3 code to demonstrate working of
# Custom length tuples from String
# Using map() + int + tuple() + list comprehension + split()
 
# initializing string
test_str = '4 6 7, 1 2, 3, 4 6 8 8'
 
# printing original string
print("The original string is : " + str(test_str))
 
# split() used to split on delimiter and
# using map() to extend logic of element casting
res = [tuple(map(int, sub.split())) for sub in test_str.split(", ")]
 
# printing result
print("The constructed custom length tuples : " + str(res))


Output

The original string is : 4 6 7, 1 2, 3, 4 6 8 8
The constructed custom length tuples : [(4, 6, 7), (1, 2), (3, ), (4, 6, 8, 8)]

Time Complexity: O(n*n) where n is the number of elements in the list “test_str”. 
Auxiliary Space: O(n) where n is the number of elements in the list “test_str”. 

Method #3: Using a loop and split() method

 step-by-step approach 

  1. Create a string test_str that contains a sequence of integers and commas.
  2. Print the original string using print(“The original string is : ” + str(test_str)).
  3. Initialize an empty list res to store the converted tuples.
  4. Split test_str using the split() method with the argument “,”. This creates a list of substrings separated by commas.
  5. Loop over each substring using a for loop and the variable name sub.
  6. Within the loop, split the substring sub into a list of integers using the split() method again, but without an argument. This creates a list of integers separated by whitespace.
  7. Convert the list of integers into a tuple using the tuple() constructor function.
  8. Append the resulting tuple to the list res using the append() method.
  9. After the loop finishes, print the resulting list of tuples using print(“The constructed custom length tuples : ” + str(res)).

Python3




# initializing string
test_str = '4 6 7, 1 2, 3, 4 6 8 8'
 
# printing original string
print("The original string is : " + str(test_str))
 
# using loop and split() to convert each substring into tuple of integers
res = []
for sub in test_str.split(","):
    tup = tuple(map(int, sub.split()))
    res.append(tup)
 
# printing result
print("The constructed custom length tuples : " + str(res))


Output

The original string is : 4 6 7, 1 2, 3, 4 6 8 8
The constructed custom length tuples : [(4, 6, 7), (1, 2), (3,), (4, 6, 8, 8)]

Time complexity: O(nm), where n is the number of substrings and m is the maximum length of each substring
Auxiliary space: O(nm), as we are creating a new list to store the resulting tuples



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