Z98n
Z98n

Reputation: 81

how to construct a matrix from lists in Python?

I'm new to Python and my problem is how to construct a matrix from some lists? For example if i have lists:

[388.625, 174.125, 157.25, 166.375]
[432.25, 606.125, 326.25, 202.75]
[383.5, 718.25, 630., 284.]
[487.375, 299.125, 438.125, 432.5]

How can i join them to create a matrix?

Upvotes: 3

Views: 33950

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 881715

Assuming that by "matrix" you mean a 2-D numpy array,

import numpy
matrix = numpy.array(list_of_lists)

will do it, if in list_of_lists you have, guess what!, the list of lists of numbers.

If what you have is, e.g, four separate lists, each of four numbers, named a, b, c and d, making a list of lists out of them isn't really all that hard...:

matrix = numpy.array([a, b, c, d])

and so forth, depending exactly on what you mean by "construct a matrix from some lists"!-)

Upvotes: 8

Related Questions