Python 2.7 在python2.7中将列表堆叠到数组中
我正在尝试解决如何将多个列表添加到一个数组中。下面是我想到的,但它不起作用Python 2.7 在python2.7中将列表堆叠到数组中,python-2.7,numpy,Python 2.7,Numpy,我正在尝试解决如何将多个列表添加到一个数组中。下面是我想到的,但它不起作用 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=f
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)
帮助在def\uu init\uuuuu中允许\uu init\uuu接受任意数量的参数:
def __init__(self, *myLists): #This could accept 1 or many lists
def __init__(self, *myLists): #This could accept 1 or many lists
屈服
[[ 1. 2. nan]
[ 3. 4. 5.]]
此外,在调试错误时,查看异常本身以外的内容也很有用。完整回溯包括发生异常的行:
---> 20 foo = myThing(myList1,myList2)
21 print foo.myVars
TypeError: __init__() takes at most 2 arguments (3 given)
这有助于我们了解问题的原因。我想你的意思是:
def __init__(self, *myLists): #This could accept 1 or many lists