如何在python中将列表转换为2 d数组

时间:2016-10-27 14:20:52

标签: python arrays list python-3.x

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

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

有没有办法将此列表提交给该功能?

1 个答案:

答案 0 :(得分:-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