
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Questions & Answers
- Matrix creation of n*n in Python
- Split tuple into groups of n in Python
- Dictionary to list of tuple conversion in Python
- How to print a matrix of size n*n in spiral order using C#?
- How to rotate a matrix of size n*n to 90 degree using C#?
- Maximum number of ones in a N*N matrix with given constraints in C++
- Raise a square matrix to the power n in Linear Algebra in Python
- Calculate n + nn + nnn + ? + n(m times) in Python
- Python Program to calculate n+nm+nmm.......+n(m times).
- Print n x n spiral matrix using O(1) extra space in C Program.
- How to rotate a matrix of size n*n to 90-degree k times using C#?
- Chunk Tuples to N in Python
- Find (1^n + 2^n + 3^n + 4^n) mod 5 in C++
- Pow(x, n) in Python
- Construct an identity matrix of order n in JavaScript
Advertisements