Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Conversion to N*N tuple matrix in Python
When it is required to convert an N*N tuple into a matrix, a simple loop and the * operator can be used.
The * operator can be used to get the product of two values. It can also be used to multiple a single value multiple times and display it on the console.
Below is a demonstration of the same −
Example
my_tuple_1 = ((11, 14), (0, 78), (33, 11), (10, 78))
print("The tuple of tuple is : ")
print(my_tuple_1)
N = 4
print("The value of N has been initialized to "+ str(N))
my_result = []
for tup in my_tuple_1 :
my_result.append( tup +(0, ) * (N - len(tup)))
print("The tuple after filling in the values is: ")
print(my_result)
Output
The tuple of tuple is : ((11, 14), (0, 78), (33, 11), (10, 78)) The value of N has been initialized to 4 The tuple after filling in the values is: [(11, 14, 0, 0), (0, 78, 0, 0), (33, 11, 0, 0), (10, 78, 0, 0)]
Explanation
- A nested tuple is defined and is displayed on the console.
- The value of 'N' is defined, and displayed.
- Another empty list is created.
- The nested tuple is iterated, and 0 is added after every value, and repeated 'N- len(tuple)' times.
- This is assigned to a value.
- It is displayed on the console.
Advertisements
