3

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?

2
  • There is a nice tutorial on dataframes in Python here: pandas.pydata.org/pandas-docs/dev/10min.html Commented Mar 24, 2015 at 3:13
  • matrix = [[ 0.4691, -0.2829, -1.5091, -1.1356], [ 1.2121, -0.1732, 0.1192, -1.0442], [-0.8618, -2.1046, -0.4949, 1.0718], [ 0.7216, -0.7068, -1.0396, 0.2719], [-0.425 , 0.567 , 0.2762, -1.0874], [-0.6737, 0.1136, -1.4784, 0.525 ]] Commented Mar 24, 2015 at 3:14

1 Answer 1

8

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"!-)

Sign up to request clarification or add additional context in comments.

1 Comment

Or numpy.matrix(list_of_lists), since OP said "matrix".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.