Python | Conversion to N*N tuple matrix
Last Updated :
11 Apr, 2023
Sometimes, while working with data, we can have a problem in which we have data in form of tuple Matrix with uneven length rows. In this case there’s a requirement to complete the N*N matrix with a default value. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using loop + * operator This problem can be solved using loop. This is brute force method to perform this task. We just append the default value as many times, as the data is missing in a row than N.
Python3
test_tup = (( 5 , 4 ), ( 3 , ), ( 1 , 5 , 6 , 7 ), ( 2 , 4 , 5 ))
print ( "The original tuple is : " + str (test_tup))
N = 4
res = []
for tup in test_tup :
res.append( tup + ( 0 , ) * (N - len (tup)))
print ( "Tuple after filling values : " + str (res))
|
Output
The original tuple is : ((5, 4), (3,), (1, 5, 6, 7), (2, 4, 5))
Tuple after filling values : [(5, 4, 0, 0), (3, 0, 0, 0), (1, 5, 6, 7), (2, 4, 5, 0)]
Method #2 : Using tuple() + generator expression Similar task can be performed in one line using generator expression. In this, similar logic is applied as above just zipped as one-liner. The tuple(), changes result to tuple.
Python3
test_tup = (( 5 , 4 ), ( 3 , ), ( 1 , 5 , 6 , 7 ), ( 2 , 4 , 5 ))
print ( "The original tuple is : " + str (test_tup))
N = 4
res = tuple (sub + ( 0 , ) * (N - len (sub)) for sub in test_tup)
print ( "Tuple after filling values : " + str (res))
|
Output
The original tuple is : ((5, 4), (3,), (1, 5, 6, 7), (2, 4, 5))
Tuple after filling values : ((5, 4, 0, 0), (3, 0, 0, 0), (1, 5, 6, 7), (2, 4, 5, 0))
Method #3 : Using map()+lambda
Approach
Using map() function
Algorithm
1. Define the original tuple and the value of N.
2. Use the map() function to apply a lambda function to each tuple in the original tuple.
3. The lambda function checks the length of the tuple and adds zeros if the length is less than N.
4. The resulting tuple is stored in the result variable.
Python3
test_tup = (( 5 , 4 ), ( 3 , ), ( 1 , 5 , 6 , 7 ), ( 2 , 4 , 5 ))
print ( "The original tuple is : " + str (test_tup))
N = 4
res = tuple ( map ( lambda t: t + ( 0 ,) * (N - len (t)), test_tup))
print ( "Tuple after filling values : " + str (res))
|
Output
The original tuple is : ((5, 4), (3,), (1, 5, 6, 7), (2, 4, 5))
Tuple after filling values : ((5, 4, 0, 0), (3, 0, 0, 0), (1, 5, 6, 7), (2, 4, 5, 0))
Time complexity: O(N*M), where N is the length of the original tuple and M is the maximum length of any tuple in the original tuple. This is because we are iterating over each tuple in the original tuple and adding zeros if necessary.
Space complexity: O(N*M), where N is the length of the original tuple and M is the maximum length of any tuple in the original tuple. This is because we are creating a new tuple with zeros appended to each tuple in the original tuple, and this new tuple has N tuples with M elements each.
METHOD 4: Using def function
APPROACH:
This approach first finds the maximum length of any tuple in the input tuple. It then loops through each tuple in the input tuple and checks if the length of the tuple is less than the maximum length. If it is, it appends the tuple with the necessary number of zeroes to make its length equal to the maximum length. Otherwise, it just appends the tuple as is. Finally, it returns the resulting matrix as a tuple.
ALGORITHM:
1.Find the maximum length of any tuple in the input tuple.
2.Initialize an empty list result.
3.Loop through each tuple x in the input tuple:
a. If the length of x is less than max_len, add (max_len – len(x)) zeros to x and append the resulting tuple to result.
b. Otherwise, simply append x to result.
4.Convert the result list to a tuple and return it.
Python3
def convert_to_matrix(tup):
max_len = max ([ len (x) for x in tup])
result = []
for x in tup:
if len (x) < max_len:
result.append(x + ( 0 ,) * (max_len - len (x)))
else :
result.append(x)
return tuple (result)
original_tuple = (( 5 , 4 ), ( 3 ,), ( 1 , 5 , 6 , 7 ), ( 2 , 4 , 5 ))
result = convert_to_matrix(original_tuple)
print (result)
|
Output
((5, 4, 0, 0), (3, 0, 0, 0), (1, 5, 6, 7), (2, 4, 5, 0))
The time complexity of this algorithm is O(nm), where n is the length of the input tuple and m is the maximum length of any tuple in the input tuple. The auxiliary space is also O(nm), as a new matrix with the same size as the input tuple is created.
Similar Reads
Python - Convert Matrix to Custom Tuple Matrix
Sometimes, while working with Python Matrix, we can have a problem in which we need to perform conversion of a Python Matrix to matrix of tuples which a value attached row-wise custom from external list. This kind of problem can have applications in data domains as Matrix is integral DS that is used
6 min read
Convert List to Tuple in Python
The task of converting a list to a tuple in Python involves transforming a mutable data structure list into an immutable one tuple. Using tuple()The most straightforward and efficient method to convert a list into a tuple is by using the built-in tuple(). This method directly takes any iterable like
2 min read
Python - Convert Matrix to overlapping Tuple Pairs
Sometimes, while working with Python data, we can have a problem in which we need to perform overlap of elements in Matrix, and convert them as tuple pairs. This kind of problem can occur in various application in data domain. Let's discuss certain ways in which this task can be performed. Input : t
5 min read
Python | List of tuples to dictionary conversion
Interconversions are always required while coding in Python, also because of the expansion of Python as a prime language in the field of Data Science. This article discusses yet another problem that converts to dictionary and assigns keys as 1st element of tuple and rest as it's value. Let's discuss
3 min read
Python - Tuple Matrix Columns Summation
Sometimes, while working with Tuple Matrix, we can have a problem in which we need to perform summation of each column of tuple matrix, at the element level. This kind of problem can have application in Data Science domains. Let's discuss certain ways in which this task can be performed. Input : tes
8 min read
Convert Tuple to Json Array in Python
Python's versatility as a programming language extends to its rich data structures, including tuples and JSON. JSON, abbreviation for JavaScript Object Notation, is a lightweight data format used for representing structured data. Moreover, it is a syntax for storing and exchanging data. In this arti
3 min read
Python - Convert Matrix to Dictionary
The task of converting a matrix to a dictionary in Python involves transforming a 2D list or matrix into a dictionary, where each key represents a row number and the corresponding value is the row itself. For example, given a matrix li = [[5, 6, 7], [8, 3, 2], [8, 2, 1]], the goal is to convert it i
4 min read
Convert Dictionary to List of Tuples - Python
Converting a dictionary into a list of tuples involves transforming each key-value pair into a tuple, where the key is the first element and the corresponding value is the second. For example, given a dictionary d = {'a': 1, 'b': 2, 'c': 3}, the expected output after conversion is [('a', 1), ('b', 2
3 min read
Python | Convert Integral list to tuple list
Sometimes, while working with data, we can have a problem in which we need to perform type of interconversions of data. There can be a problem in which we may need to convert integral list elements to single element tuples. Let's discuss certain ways in which this task can be performed. Method #1 :
3 min read
Convert tuple to string in Python
The goal is to convert the elements of a tuple into a single string, with each element joined by a specific separator, such as a space or no separator at all. For example, in the tuple ('Learn', 'Python', 'Programming'), we aim to convert it into the string "Learn Python Programming". Let's explore
3 min read