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
. So I cant use np.asarray()
.
最重要的挑战是我不会导入numpy。所以我不能使用np.asarray()。
I have also used array.fromlist()
and 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?
我还使用了array.fromlist()和array.extend()。两种方法都在创建单维数组。但我想将多维列表转换为多维数组。有什么办法吗?
1 个解决方案
#1
3
What you're asking is not possible. From the array
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
该模块定义了一个对象类型,它可以紧凑地表示一个基本值数组:字符,整数,浮点数......类型是在对象创建时使用类型代码指定的
The only supported types are as follows (notice the absence of 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. This is not the case.
看起来你可能会混淆数组模块,使用numpy数组的本机实现。不是这种情况。
#1
3
What you're asking is not possible. From the array
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
该模块定义了一个对象类型,它可以紧凑地表示一个基本值数组:字符,整数,浮点数......类型是在对象创建时使用类型代码指定的
The only supported types are as follows (notice the absence of 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. This is not the case.
看起来你可能会混淆数组模块,使用numpy数组的本机实现。不是这种情况。