0

I'm trying to work out how to add multiple lists to an array. Following is what I've come up with but it's not working.

import math
import numpy as np

myList1 = [1,2,'nan']
myList2 = [3,4,5]

class myThing(object):
    def __init__(self, myLists=[]): #This could accept 1 or many lists
    self.__myVars = np.array(myLists, dtype=float)
    self.__myVars.shape = (len(myLists),3)
    self.__myVars = np.vstack(myVars)

        @property
    def myVars(self):
        return self.__myVars

foo = myThing(myList1,myList2)
print foo.myVars

blah blah...
TypeError: __init__() takes at most 2 arguments (3 given)

Help appreciated

mark
  • 537
  • 6
  • 25

2 Answers2

0

Use *myLists in def __init__ to allow __init__ to accept an arbitrary number of arguments:

def __init__(self, *myLists): #This could accept 1 or many lists

import numpy as np

myList1 = [1,2,'nan']
myList2 = [3,4,5]

class myThing(object):
    def __init__(self, *myLists): #This could accept 1 or many lists
        self.__myVars = np.array(myLists, dtype=float)

    @property
    def myVars(self):
        return self.__myVars

foo = myThing(myList1,myList2)
print foo.myVars

yields

[[  1.   2.  nan]
 [  3.   4.   5.]]

Also, when debugging errors, it's useful to look at more than the exception itself. The full traceback includes the line on which the Exception occurred:

---> 20 foo = myThing(myList1,myList2)
     21 print foo.myVars

TypeError: __init__() takes at most 2 arguments (3 given)

This can help clue us in to what is causing the problem.

Community
  • 1
  • 1
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
0

I think you mean:

def __init__(self, *myLists): #This could accept 1 or many lists
xnx
  • 24,509
  • 11
  • 70
  • 109