How to select 2 elements from a list such that it represents the size of a matrix?
Question:
I was solving the matrix chain problem and I’ve encountered a dead end.
I have the values of p0,p1,p2,p3,p4,p5
as 4,10,3,12,20,7
respectively
now I want to mark that p0
and p1
combine together to form a matrix of [4][10]
size. Similarly p1
and p2
combine to form a matrix of [10][3]
size.
please help me in python language
Answers:
p_list = [p0,p1,p2,p3,p4,p5] = [4,10,3,12,20,7]
m_list = []
for a, b in zip(p_list, p_list[1:]):
m_list.append([a, b])
m1, m2, m3, m4, m5 = m_list
you can create a list of the ps then zip together the 0 to n-1
elements with 1 to n
and list them:
p = [4,10,3,12,20,7]
m1, m2, m3, m4, m5 = list( zip( p[:-1],p[1:] ) )
I was solving the matrix chain problem and I’ve encountered a dead end.
I have the values of p0,p1,p2,p3,p4,p5
as 4,10,3,12,20,7
respectively
now I want to mark that p0
and p1
combine together to form a matrix of [4][10]
size. Similarly p1
and p2
combine to form a matrix of [10][3]
size.
please help me in python language
p_list = [p0,p1,p2,p3,p4,p5] = [4,10,3,12,20,7]
m_list = []
for a, b in zip(p_list, p_list[1:]):
m_list.append([a, b])
m1, m2, m3, m4, m5 = m_list
you can create a list of the ps then zip together the 0 to n-1
elements with 1 to n
and list them:
p = [4,10,3,12,20,7]
m1, m2, m3, m4, m5 = list( zip( p[:-1],p[1:] ) )