-3

I am using python, I have a list which contains inside it group of lists how can I convert it into one matrix?

for example,

Root_List = [list1  list2  list3]
list1 = [1 2 3]
list2 = [1 5 9]
list3 = [2 4 1]

I need matrix to have below value

[
1 2 3
1 5 9
2 4 1
]

Any idea? Thank you in advanced.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Miss.lo0ora
  • 21
  • 1
  • 4

2 Answers2

2

If they all have the same length, try this:

import numpy as np
list1 = [1,2,3]
list2 = [1,5,9]
list3 = [2,4,1]
Root_List = [list1, list2,list3]
np.array(Root_List)
Manrique
  • 2,083
  • 3
  • 15
  • 38
0

A matrix is a list of list.

Best practice is to first define your root list, then append sublists you want:

Root_List = []
list1 = [1, 2, 3]
Root_List.append(list1)
list2 = [1, 5, 9]
Root_List.append(list2)
list3 = [2, 4, 1]
Root_List.append(list3)

As suggested by @Antonio Manrique, you can also convert it using numpy to benefit matrix calculation functions of this lib. This answer answer how to convert a python list of list to a numpy matrix.

hayj
  • 1,159
  • 13
  • 21