Q:

Python program to perform row-wise element addition in Tuple matrix

belongs to collection: Python Tuple Programs

0

Example:

tuple = ("python", "includehelp", 43, 54.23)

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of an ordered set of values enclosed in square brackets []

Example:

list = [3 ,1,  5, 7]

Performing row-wise element addition in Tuple matrix

We have a tuples matrix consisting of values and we need to perform addition which is row-wise on the matrix.

Input:
tupMat = [[(7, 2, 6), (4, 1, 5)], [(9, 2, 6), (4, 5, 3)]]

addMat = [3 , 2]

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

We will be taking each row and adding values to it from another tuple. In Python, we have multiple methods and ways to perform the task.

Let's see some of these methods,

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Method 1:

A method to solve the problem is by using nested list comprehension along with enumerate() method. We will use enumerate() to insert values to tuples of the tuple matrix.

# Python program to perform Row-wise element 
# addition on tuple matrix 

# Creating and printing tuple matrix 
tupMat = [[(7, 2, 6), (4, 1, 5)], [(9, 2, 6), (4, 5, 3)]]
additionVals = [3, 2]
print("Elements of Tuple matrix initially :" + str(tupMat))

# Performing Row-wise element addition operation on tuple matrix 
addTupMat = [[sub + (additionVals[i], ) for sub in ele] for i, ele in enumerate(tupMat)]

# printing the added matrix 
print("Elements of Tuple matrix after addition :" + str(addTupMat))

Output:

Elements of Tuple matrix initially :[[(7, 2, 6), (4, 1, 5)], [(9, 2, 6), (4, 5, 3)]]
Elements of Tuple matrix after addition :[[(7, 2, 6, 3), (4, 1, 5, 3)], [(9, 2, 6, 2), (4, 5, 3, 2)]]

Method 2:

Another method to create the program for addition is by using the zip() method along with list comprehension to zip together the element to be added in with the tuples of the matrix. We will do this with each row to add elements to them.

# Python program to perform Row-wise element 
# addition on tuple matrix 

# Creating and printing tuple matrix 
tupMat = [[(7, 2), (4, 1, 5)], [(9, 2, 6), (4, 5, 3)]]
additionVals = [3, 2]
print("Elements of Tuple matrix initially :" + str(tupMat))

# Performing Row-wise element addition operation on tuple matrix 
addTupMat = [[(idx, val) for idx in key] for key, val in zip(tupMat, additionVals)]

# printing the added matrix 
print("Elements of Tuple matrix after addition :" + str(addTupMat))

Output:

Elements of Tuple matrix initially :[[(7, 2), (4, 1, 5)], [(9, 2, 6), (4, 5, 3)]]
Elements of Tuple matrix after addition :[[((7, 2), 3), ((4, 1, 5), 3)], [((9, 2, 6), 2), ((4, 5, 3), 2)]]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to get even indexed elements in tup... >>
<< Python program to perform cross tuple summation gr...