8

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

 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

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

3
  • Give an example of your three lists and of your three-dimensional numpy array you want to obtain. Commented Mar 27, 2012 at 9:30
  • @eumiro right now I am using a test case where the lists are [0,0,0,0], [3,4,4,3], [3,4,3,4] what I would like is list one the first column list two the second list three the third. This will eventually involve three very large lists that need to be converted into one array for analysis. Thank you Commented Mar 27, 2012 at 9:56
  • 2
    You'll be getting a two-dimensional array, not three dimensional. The length of the major dimension will be three. Commented Mar 27, 2012 at 10:17

2 Answers 2

7

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.array(object, 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.

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

>>> 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)
Sign up to request clarification or add additional context in comments.

6 Comments

Tried to give the tuple, but got a tuple not callable error
Then your lists aren't what you say they are or what you expect them to be. This works fine for me with your example lists given in your comment to your question (see my edit).
I just printed on of the lists and this is what I got ['0', '0', '0', '0']. I think the quotation marks maybe the problem what do you think?
@Surfcast23 - It sounds like you're doing x.shape(). It's not a function, it's a tuple. You can't call it. It's just shape = x.shape.
@JoeKington Agreed. @Surfcast23 You can modify the shape of an array by using A.shape = (3, 3) rather than A.shape(3,3). However, you don't need to use this at all in your code in your question. In fact, in your example you are reshaping an array to an array with a different number of elements, which would raise a ValueError exception.
|
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.

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 Comment

Never mind the fact that import * is bad practice, what's the point of import numpy followed immediately by from numpy import * ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.