【问题标题】:How to convert a list to 2 d array in python [duplicate]如何在python中将列表转换为二维数组[重复]
【发布时间】:2016-10-27 14:20:51
【问题描述】:

我正在使用一个函数 X ,它采用 np.ndarray, shape=(num_pairs, 2), dtype=int values 。 我只有一个列表l=[a,b],我想将此列表作为输入提供给上述函数 X。

我收到错误,因为 * l 必须是 ndim 2。你提供了 1 *

有没有办法将此列表提交给函数?

【问题讨论】:

    标签: python arrays list python-3.x


    【解决方案1】:

    this answer

    >>> import numpy as np
    >>> l = np.array([1,2])
    >>> l
    array([1, 2])
    >>> l.shape
    (2,)
    >>> l.ndim
    1
    >>> l[None, :].shape
    (1, 2)
    >>> l[None, :].ndim
    2
    

    【讨论】:

    • 如果您所拥有的只是对另一个答案的引用,请投票以关闭该问题作为该问题的副本。
    猜你喜欢
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 2011-12-04
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    相关资源
    最近更新 更多