如何从Python列表中构造矩阵? [英] how to construct a matrix from lists in Python?
问题描述
我是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?
推荐答案
假设矩阵"是指二维numpy
数组,
Assuming that by "matrix" you mean a 2-D numpy
array,
import numpy
matrix = numpy.array(list_of_lists)
会这样做,如果在list_of_lists
中,请猜猜是什么,数字列表的列表.
will do it, if in list_of_lists
you have, guess what!, the list of lists of numbers.
如果您拥有的是例如四个单独的列表,则四个数字中的每个数字分别命名为a
,b
,c
和d
,实际上并不是所有列表都包含在内这么难...
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"!-)
这篇关于如何从Python列表中构造矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!