2. (20 pts) Consider the list of lists object created by the following statement
ID: 3870117 • Letter: 2
Question
2. (20 pts) Consider the list of lists object created by the following statement: This creates the equivalent of a 3-by-3 matrix. For each of the following, give the Python statement to create the requested value(s). Note that each can be done with a single statement and I want you to extract the values from M- do not use literals. a. A list consisting of the 3rd column of the matrix ( 3, , , 14 , , 91' ] ) b. A list of integer values for the elements in the 3rd row of the matrix ([16, 22, 91]) c. The diagonal from the upper left to the lower right of the matrix (I '', 12, 91 d. The upper left two-by-two matrix from M(II'1', 2', ['18, '])Explanation / Answer
M = [['1','2','3'],['8','12','14'],['16','22','91']]
print(M)
#create a 3rd column matrix
c3 = [row[2] for row in M]
print(c3)
# create row 3.
r3 = M[2]
print(r3)
# generate upper diagonal matrix
d1 = [M[i][i] for i in range(len(M))]
print(d1)
# generte 2 by 2 matrix of first 2 rows
twobytwo = [M[i][:2] for i in range(2)]
print(twobytwo)
"""
sample output
[['1', '2', '3'], ['8', '12', '14'], ['16', '22', '91']]
['3', '14', '91']
['16', '22', '91']
['1', '12', '91']
[['1', '2'], ['8', '12']]
"""
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.