Home > Enterprise >  How can i store the list elements in first row and first colomn of the matrix in python
How can i store the list elements in first row and first colomn of the matrix in python

Time:03-12

I want to store the list elements as the first row and colomn in a matrix in python

myLIST=[41,32,49,4,55]

after that i want to create a matrix of size =(m,n) where m and n = length of (myLIST); such that:

myMATRIX=[ [41,32,49,4,55], [32,0,0,0,0] ,[49,0,0,0,0], [4,0,0,0,0], [55,0,0,0,0] ]

CodePudding user response:

You can use list comprehension:

lst = [41,32,49,4,55]

n = len(lst)
output = [[lst[max(i, j)] if i * j == 0 else 0 for j in range(n)] for i in range(n)]

print(output)
# [[41, 32, 49, 4, 55], [32, 0, 0, 0, 0], [49, 0, 0, 0, 0], [4, 0, 0, 0, 0], [55, 0, 0, 0, 0]]

Normally the pattern for ... in range(len(...)) is ugly, but in this case I find it better than using for ... in enumerate(...).

Note that we are assigning a value from lst when and only when i * j == 0, i.e., at the first row or at the first column; this is done by ... if ... else ....

CodePudding user response:

List comprehensions can be very useful but sometimes they become so complex/unreadable that IMO it's better to use for loops for the sake of clarity at the possible cost of efficiency. Never forget that someone else may need to amend your code one day and they could spend an awful lot of time unravelling your list/dictionary/set comprehension.

Having said that, you could do this:

myLIST = [41, 32, 49, 4, 55]
myMATRIX = [myLIST]   [[e]   [0] * (len(myLIST)-1) for e in myLIST[1:]]
print(myMATRIX)

Output:

[[41, 32, 49, 4, 55], [32, 0, 0, 0, 0], [49, 0, 0, 0, 0], [4, 0, 0, 0, 0], [55, 0, 0, 0, 0]]

CodePudding user response:

Using only list comprehensions

Here is one way you can do this using enumerate. The logic is as below -

  1. Iterate over each element of the myLIST with enumerate to get their index position.
  2. If position is greater than 0th index, return the specific element along with a list of 0s
  3. Else, return the complete list when index is 0.
[[myLIST[i]] ([0]*(len(myLIST)-1)) if i>0 else myLIST for i,j in enumerate(myLIST)]
[[41, 32, 49, 4, 55],
 [32, 0, 0, 0, 0],
 [49, 0, 0, 0, 0],
 [4, 0, 0, 0, 0],
 [55, 0, 0, 0, 0]]

Since your goal is to master list comprehensions, I would recommend the following guides that I found useful long back with the same -

  1. https://www.programiz.com/python-programming/list-comprehension
  2. https://www.analyticsvidhya.com/blog/2021/06/10-examples-you-should-try-to-master-list-comprehensions-in-python/

Using a combination of numpy and list comprehension

For completeness, you can do this purely in numpy by using stride_tricks or np.roll as well.

Check this - Roll rows of a matrix independently

Another approach is to use a combination of np.roll and list comprehension with ternary operator conditions on the index -

  1. Convert myLIST to a diagonal matrix using np.diag(myLIST)
array([[41,  0,  0,  0,  0],
       [ 0, 32,  0,  0,  0],
       [ 0,  0, 49,  0,  0],
       [ 0,  0,  0,  4,  0],
       [ 0,  0,  0,  0, 55]])
  1. Next, you can then np.roll each of the of the rows in the array independently by the index number * -1. So, for [ 0, 0, 49, 0, 0], -2 roll would result in [ 49, 0, 0, 0, 0]

  2. Put a condition to roll every thing with index > 0 and when index is 0, just return the original array.

Here is the complete code as a list comprehension -

import numpy

arr = np.diag(myLIST)

[list(np.roll(j,-1*i)) if i>0 else myLIST for i,j in enumerate(arr)]
[[41, 32, 49, 4, 55],
 [32, 0, 0, 0, 0],
 [49, 0, 0, 0, 0],
 [4, 0, 0, 0, 0],
 [55, 0, 0, 0, 0]]
  • Related