在Python中将列表转换为列向量 [英] Transforming a list to a column vector in Python
本文介绍了在Python中将列表转换为列向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有一个函数将列表X
作为输入并输出对应于X
的列向量?
I was wondering if there's a function that takes a list X
as input and outputs a column vector corresponding to X
?
(我环顾四周,我怀疑可能是np.matrix(X).T
)
(I looked around and I suspect it might be: np.matrix(X).T
)
推荐答案
我认为没有功能,但是有一个专用对象np.c_
I don't think there is a function, but there is a dedicated object, np.c_
>>> X = list('hello world')
>>> X
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
np.c_[X]
array([['h'],
['e'],
['l'],
['l'],
['o'],
[' '],
['w'],
['o'],
['r'],
['l'],
['d']], dtype='<U1')
您也可以这样做(请注意额外的方括号)
You can also do (note the extra square brackets)
>>> np.array([X]).T
这篇关于在Python中将列表转换为列向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文