Transforming a list to a column vector in Python

前端 未结 4 1983
面向向阳花
面向向阳花 2021-01-21 05:33

I was wondering if there\'s a function that takes a list X as input and outputs a column vector corresponding to X?

(I looked around and I susp

4条回答
  •  时光取名叫无心
    2021-01-21 05:38

    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='

    You can also do (note the extra square brackets)

    >>> np.array([X]).T
    

提交回复
热议问题