如何将2d列表转换为2d原生python数组而不是numpy数组?

2024-06-28 19:48:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我想转换python2d列表

[[1,2,3],[4,5,6]]  

Python3array.array

^{pr2}$

最重要的挑战是我不会导入numpy。 所以我不能使用np.asarray()。在

我还使用了array.fromlist()和{}。 两种方法都在创建一维数组。 但是我想把多维列表转换成多维数组。 有什么办法吗?在


Tags: 方法numpy列表np数组arrayasarray办法
1条回答
网友
1楼 · 发布于 2024-06-28 19:48:58

你的要求是不可能的。来自the ^{} docs

This module defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers ... The type is specified at object creation time by using a type code

仅支持的类型如下(注意没有list):

Type code            C Type        Python Type
    'b'         signed char                int
    'B'       unsigned char                int
    'u'          Py_UNICODE  Unicode character
    'h'        signed short                int
    'H'      unsigned short                int
    'i'          signed int                int
    'I'        unsigned int                int
    'l'         signed long                int
    'L'       unsigned long                int
    'q'    signed long long                int
    'Q'  unsigned long long                int
    'f'               float              float
    'd'              double              float 

似乎您可能混淆了array模块与numpy数组的本机实现。情况并非如此。在

相关问题 更多 >