简体   繁体   English

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

[英]how to convert 2d list to 2d native python array not numpy array?

I want to convert python 2d list, 我想转换python 2d列表,

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

to

python3 array.array type, python3 array.array类型,

[ [1 2 3]

[4 5 6] ]

The most important challenge is I wont import numpy . 最重要的挑战是我不会导入numpy So I cant use np.asarray() . 所以我不能使用np.asarray()

I have also used array.fromlist() and array.extend() . 我还使用了array.fromlist()array.extend() Both methods are creating an array of single dimension. 这两种方法都在创建单维数组。 But I want to convert multi dimension list to multi dimension array. 但我想将多维列表转换为多维数组。 Is there any way? 有什么办法吗?

What you're asking is not possible. 你问的是不可能的。 From the array docs : array文档

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 该模块定义了一个对象类型,它可以紧凑地表示一个基本值数组:字符,整数,浮点数......通过使用类型代码在对象创建时指定类型

The only supported types are as follows (notice the absence of list ): 唯一支持的类型如下(注意缺少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 

It seems like you might be confusing the array module, with a native implementation of numpy arrays. 看起来你可能会混淆array模块,使用numpy数组的本机实现。 This is not the case . 事实并非如此

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM