0

Hi all!!!
I have some problems with List comprehension in Python.
How can I obtain these two nested list using comprehension?

list1 = [[1,2,3],[4,5,6],[7,8,9]]
list2 = [[1,0,0],[0,1,0],[0,0,1]] (identity matrix)

where list1 and list2 are square matrix of 'n' size

thank you in advance!!!

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by prexioso
0

for n=3:

list1=[[member*3+1+i for i in range(3)] for member in range(3)]
list2=[[1 if i==member else 0 for i in range(3)] for member in range(3)]

Modifying for any n is left to the user....

0

And which one you think is better name for variable list2 or identity_matrix? How about square or list1?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.