Python Program to convert a list into matrix with size of each row increasing by a number
Last Updated :
18 May, 2023
Given a list and a number N, the task here is to write a python program to convert it to matrix where each row has N elements more than previous row elements from list.
Input : test_list = [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1], N = 3
Output : [[4, 6, 8], [4, 6, 8, 1, 2, 9], [4, 6, 8, 1, 2, 9, 0, 10, 12], [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]]
Explanation : Each row has 3 elements more than previous row.
Input : test_list = [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1], N = 4
Output : [[4, 6, 8, 1], [4, 6, 8, 1, 2, 9, 0, 10], [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]]
Explanation : Each row has 4 elements more than previous row.
Method 1: Using loop and slicing
In this, we perform the task of getting chunks using slicing, and the conversion of the list into a Matrix is done using a loop.
Program:
Python3
test_list = [ 4 , 6 , 8 , 1 , 2 , 9 , 0 , 10 , 12 , 3 , 9 , 1 ]
print ( "The original list is : " + str (test_list))
N = 3
res = []
for idx in range ( 0 , len (test_list) / / N):
res.append(test_list[ 0 : (idx + 1 ) * N])
print ( "Constructed Chunk Matrix : " + str (res))
|
Output
The original list is : [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]
Constructed Chunk Matrix : [[4, 6, 8], [4, 6, 8, 1, 2, 9], [4, 6, 8, 1, 2, 9, 0, 10, 12], [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]]
Time Complexity: O(m*n), where m is the row and n is the column of the matrix
Auxiliary Space: O(k), where k is the size of the matrix
Method 2: Using list comprehension and list slicing
In this, we perform the task of setting values using list comprehension as a shorthand. Rest all the operations are done similarly to the above method.
Program:
Python3
test_list = [ 4 , 6 , 8 , 1 , 2 , 9 , 0 , 10 , 12 , 3 , 9 , 1 ]
print ( "The original list is : " + str (test_list))
N = 3
res = [test_list[ 0 : (idx + 1 ) * N] for idx in range ( 0 , len (test_list) / / N)]
print ( "Constructed Chunk Matrix : " + str (res))
|
Output
The original list is : [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]
Constructed Chunk Matrix : [[4, 6, 8], [4, 6, 8, 1, 2, 9], [4, 6, 8, 1, 2, 9, 0, 10, 12], [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]]
Time Complexity: O(m*n)
Auxiliary Space: O(k)
Method 3: Using the numpy library’s reshape() method.
Here’s the step-by-step approach:
- Import the numpy library.
- Initialize the input list and the chunk size.
- Convert the input list to a numpy array.
- Reshape the array to have N columns and as many rows as necessary.
- Convert the chunk matrix back to a Python list.
- Print the result.
Python3
import numpy as np
test_list = [ 4 , 6 , 8 , 1 , 2 , 9 , 0 , 10 , 12 , 3 , 9 , 1 ]
print ( "The original list is : " + str (test_list))
N = 3
arr = np.array(test_list)
chunk_matrix = arr.reshape( - 1 , N)
res = chunk_matrix.tolist()
print ( "Constructed Chunk Matrix : " + str (res))
|
OUTPUT:
The original list is : [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]
Constructed Chunk Matrix : [[4, 6, 8], [1, 2, 9], [0, 10, 12], [3, 9, 1]]
Time complexity: O(n), where n is the size of the input list.
Auxiliary space: O(n) for the numpy array, but it is converted back to a Python list, so the overall space complexity is also O(n).
Method 4: Using a recursive function
- Create a list called test_list containing 12 integers.
- Initialize a variable called N to 3, which will be used as the chunk size.
- Define a function called chunk_list
- In the chunk_list function, check if the length of the list is less than or equal to the chunk size. If so, return a list containing the original list.
- If the length of the list is greater than the chunk size, slice the list from the beginning to the chunk size and append it to a new list using the + operator. Then recursively call the chunk_list function with the remaining part of the list until the sublist is less than or equal to the chunk size.
- Call the chunk_list function with test_list and N as arguments and assign the result to a variable called res.
- Print the constructed chunk matrix using the print() function, the string “Constructed Chunk Matrix: “, and the str() function to convert the result to a string.
Python3
test_list = [ 4 , 6 , 8 , 1 , 2 , 9 , 0 , 10 , 12 , 3 , 9 , 1 ]
print ( "The original list is : " + str (test_list))
N = 3
def chunk_list(lst, n):
if len (lst) < = n:
return [lst]
else :
return [lst[:n]] + chunk_list(lst[n:], n)
res = chunk_list(test_list, N)
print ( "Constructed Chunk Matrix : " + str (res))
|
Output
The original list is : [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]
Constructed Chunk Matrix : [[4, 6, 8], [1, 2, 9], [0, 10, 12], [3, 9, 1]]
Time complexity: O(N * (L/N)) = O(L), where L is the length of the original list and N is the chunk size.
Auxiliary space: O(L), as it creates a new list for each chunk of the original list.
Method 5: Using the itertools module
can use the zip_longest function from the itertools module to chunk the list into sublists of size N. This method fills the last sublist with a specified value (in this case, None) if its length is less than N.
Python3
from itertools import zip_longest
test_list = [ 4 , 6 , 8 , 1 , 2 , 9 , 0 , 10 , 12 , 3 , 9 , 1 ]
print ( "The original list is: " + str (test_list))
N = 3
def chunk_list(lst, n):
return [ list ( filter ( None , sublist)) for sublist in zip_longest( * [ iter (lst)] * n)]
res = chunk_list(test_list, N)
print ( "Constructed Chunk Matrix: " + str (res))
|
Output
The original list is: [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]
Constructed Chunk Matrix: [[4, 6, 8], [1, 2, 9], [10, 12], [3, 9, 1]]
Time Complexity: O(N * M), where N is the length of the list and M is the chunk size.
Auxiliary Space: O(N), where N is the length of the list, as the result is stored in a new list.
Similar Reads
Python Program to sort rows of a matrix by custom element count
Given Matrix, the following program shows how to sort rows of a matrix by the count of presence of numbers from a specified list. Input : test_list = [[4, 5, 1, 7], [6, 5], [9, 8, 2], [7, 1]], cus_list = [4, 5, 7] Output : [[9, 8, 2], [6, 5], [7, 1], [4, 5, 1, 7]] Explanation : 0 < 1 = 1 < 3 i
5 min read
Python program to create a list of tuples from given list having number and its cube in each tuple
We are given a list of numbers and our task is to create a list of tuples where each tuple contains a number and its cube. For example, if the input is [1, 2, 3], the output should be [(1, 1), (2, 8), (3, 27)]. Using List ComprehensionList comprehension is one of the most efficient ways to achieve t
3 min read
Python program to Convert a Matrix to Sparse Matrix
Converting a matrix to a sparse matrix involves storing only non-zero elements along with their row and column indices to save memory. Using a DictionaryConverting a matrix to a sparse matrix using a dictionary involves storing only the non-zero elements of the matrix, with their row and column indi
2 min read
Python program to convert a list into a list of lists using a step value
Given a List, the task here is to write a python program that can split the list into list of lists using by a step value here denoted via K. Input : test_list = [5, 6, 3, 2, 7, 1, 9, 10, 8], K = 3Output : [[5, 2, 9], [6, 7, 10], [3, 1, 8]]Explanation : 5, 2 and 9 are 0th, 3rd and 6th element respec
4 min read
Python Program to test whether the length of rows are in increasing order
Given a Matrix, the following program is used to test if length all rows of a matrix are in increasing order or not. If yes, it returns True, otherwise False. Input : test_list = [[3], [1, 7], [10, 2, 4], [8, 6, 5, 1, 4]] Output : True Explanation : 1 < 2 < 3 < 5, increasing lengths of rows
4 min read
Python Program that prints the rows of a given length from a matrix
Given a Matrix, the following articles shows how to extract all the rows with a specified length. Input : test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]], K = 3 Output : [[1, 4, 6], [7, 3, 1]] Explanation : Extracted lists have length of 3.Input : test_list = [[3, 4, 5, 6], [1
4 min read
Python - Sort Matrix by Number of elements greater than its previous element
Given a Matrix, sort by occurrences where next element is greater than current. Compute the count of i < i + 1 in each list, sort each row by count of each of this condition in each row. Input : test_list = [[4, 6, 2, 9, 10], [5, 3, 2, 5], [2, 4, 5, 6, 7, 7], [6, 3, 2]] Output : [[6, 3, 2], [5, 3
4 min read
Python program to find Successive row difference in Matrix
Given a Matrix, the task is to write a Python program to perform differences from the previous row on the basis of the elements present. Input : test_list = [[5, 6, 3, 1], [7, 5, 3, 1], [3, 2], [7, 3, 3, 2], [2, 3], [9, 8, 1]] Output : [[], [7], [2], [7], [], [8, 9, 1]] Explanation : Comparing 1st a
7 min read
Python program to a Sort Matrix by index-value equality count
Given a Matrix, the task is to write a Python program that can sort its rows or columns on a measure of the number of values equal to its index number. For each row or column, count occurrences of equality of index number with value. After computation of this count for each row or column, sort the m
6 min read
Python Program to Construct n*m Matrix from List
We are given a list we need to construct a n*m matrix from that list. For example, a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] we need to construct a 3*4 matrix so that resultant output becomes [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] . Using List ComprehensionThis method uses list comprehensio
3 min read