-1

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] ]
Saumy tiwari
  • 57
  • 1
  • 5
  • What have you tried? This is pretty basic Python coding. – Tim Roberts Mar 12 '22 at 07:31
  • I have a matrix with all values 0; now i want to interchange the 1st row and 1st column of the matrix with the elements of a list which is given – Saumy tiwari Mar 12 '22 at 07:34
  • Yes, we know. What code do you have? Are you using numpy? – Tim Roberts Mar 12 '22 at 07:37
  • No i am thinking about if we can do using list comprehension... i don't use external libraries as i am new in CP – Saumy tiwari Mar 12 '22 at 07:47
  • If you already HAVE a matrix of zeros, then no, you don't need a list comprehension. You need a simple `for` loop. – Tim Roberts Mar 12 '22 at 07:48
  • i mean yes we can do...but i am trying to learn list comprehension..i am thinking if we can solve it using the list comprehension only? – Saumy tiwari Mar 12 '22 at 07:50
  • I have added an approach with list comprehension and ternary operator which lets you add conditions to each element in a list comprehension. Hope that gives you some guidance in your quest to learn list comprehensions. – Akshay Sehgal Mar 12 '22 at 08:53
  • You need to use the right tool for the job. You don't use a screwdriver to drive a nail just because you want to learn the screwdriver. `for i in range(5):` / `myMATRIX[i,0] = myMATRIX[0,i] = myLIST[i]` – Tim Roberts Mar 12 '22 at 19:25

3 Answers3

0

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 ....

j1-lee
  • 13,764
  • 3
  • 14
  • 26
0

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]]
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
0

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]]
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51