如何将3个列表转换为1个3D Numpy数组?

时间:2021-08-12 21:27:54

I have three lists that I want to convert into one list. When I try the following a get this error

我有三个列表,我想把它们转换成一个列表。当我尝试下面的a得到这个错误。

 A = numpy.array(X,Y,Z,dtype=float)
 ValueError: only 2 non-keyword arguments accepted

I did not see anything here that says you can only give it two arguments

我在这里没有看到任何说你只能给它两个参数的东西。

http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html

http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html

Here is the code

这是代码

import numpy
from numpy import *

X = []
Y = []
Z = []

f = open(r'C:\My.txt')
f.readline()
for line in f:
 if line != '':
     line = line.strip()
     columns = line.split()
     x = columns[2]
     y = columns[3]
     z = columns[4]
     X.append(x)
     Y.append(y)                #appends data in list
     Z.append(z)



A = numpy.array(X,Y,Z,dtype=float)
A.shape(3,3)
print(A)

Thanks in advanceh

由于advanceh

2 个解决方案

#1


4  

Try passing your three lists as a tuple:

试着把你的三个清单作为一个元组:

A = numpy.array((X, Y, Z), dtype=float)

In the numpy.array documentation the signature for numpy.array is

numpy。数组文档:numpy的签名。数组

numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0, maskna=None, ownmaskna=False)

numpy。数组(对象,dtype=None, copy=True, order=None, subok=False, ndmin=0, maskna=None, ownmaskna=False)

i.e. the single argument object is what gets turned into an ndarray, every other argument must be a keyword argument (hence the error message which you were getting) which can be used to customise the creation of the array.

也就是说,单个参数对象被转换为ndarray,所有其他参数都必须是一个关键字参数(因此您得到的错误消息),它可以用来定制数组的创建。

Edit In respone to Surfcast23's comment, in the IDE I tried the following:

在respone中对Surfcast23的评论进行编辑,在IDE中,我尝试了以下方法:

>>> import numpy

>>> x = [0, 0, 0, 0]
>>> y = [3, 4, 4, 3]
>>> z = [3, 4, 3, 4]

>>> A = numpy.array((x, y, z), dtype=float)
>>> A
array([[ 0., 0., 0., 0.],
       [ 3., 4., 4., 3.],
       [ 3., 4., 3., 4.]])
>>> A.shape
(3L, 4L)

#2


0  

I went through your code and found out that there is a missing [] for X,Y,Z. The array cannot take two D array as one. Try putting [X,Y,Z] for the array and you shall get the correct answer.

我检查了你的代码,发现X,Y,Z有一个缺失。数组不能以两个D数组作为一个数组。试着为数组输入[X,Y,Z],你将得到正确的答案。

import numpy
from numpy import *

X = []
Y = []
Z = []

f = open(r'C:\My.txt')
f.readline()
for line in f:
 if line != '':
     line = line.strip()
     columns = line.split()
     x = columns[2]
     y = columns[3]
     z = columns[4]
     X.append(x)
     Y.append(y)                #appends data in list
     Z.append(z)



A = numpy.array([X,Y,Z],dtype = float)
A.shape(3,3)
print(A)

#1


4  

Try passing your three lists as a tuple:

试着把你的三个清单作为一个元组:

A = numpy.array((X, Y, Z), dtype=float)

In the numpy.array documentation the signature for numpy.array is

numpy。数组文档:numpy的签名。数组

numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0, maskna=None, ownmaskna=False)

numpy。数组(对象,dtype=None, copy=True, order=None, subok=False, ndmin=0, maskna=None, ownmaskna=False)

i.e. the single argument object is what gets turned into an ndarray, every other argument must be a keyword argument (hence the error message which you were getting) which can be used to customise the creation of the array.

也就是说,单个参数对象被转换为ndarray,所有其他参数都必须是一个关键字参数(因此您得到的错误消息),它可以用来定制数组的创建。

Edit In respone to Surfcast23's comment, in the IDE I tried the following:

在respone中对Surfcast23的评论进行编辑,在IDE中,我尝试了以下方法:

>>> import numpy

>>> x = [0, 0, 0, 0]
>>> y = [3, 4, 4, 3]
>>> z = [3, 4, 3, 4]

>>> A = numpy.array((x, y, z), dtype=float)
>>> A
array([[ 0., 0., 0., 0.],
       [ 3., 4., 4., 3.],
       [ 3., 4., 3., 4.]])
>>> A.shape
(3L, 4L)

#2


0  

I went through your code and found out that there is a missing [] for X,Y,Z. The array cannot take two D array as one. Try putting [X,Y,Z] for the array and you shall get the correct answer.

我检查了你的代码,发现X,Y,Z有一个缺失。数组不能以两个D数组作为一个数组。试着为数组输入[X,Y,Z],你将得到正确的答案。

import numpy
from numpy import *

X = []
Y = []
Z = []

f = open(r'C:\My.txt')
f.readline()
for line in f:
 if line != '':
     line = line.strip()
     columns = line.split()
     x = columns[2]
     y = columns[3]
     z = columns[4]
     X.append(x)
     Y.append(y)                #appends data in list
     Z.append(z)



A = numpy.array([X,Y,Z],dtype = float)
A.shape(3,3)
print(A)