Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将列表类型转换为我自己的自定义矩阵类?_Python_Python 2.7_Matrix - Fatal编程技术网

Python 如何将列表类型转换为我自己的自定义矩阵类?

Python 如何将列表类型转换为我自己的自定义矩阵类?,python,python-2.7,matrix,Python,Python 2.7,Matrix,我有一个矩阵类: class Matrix(object): def __init__(self,row,col): self.row=row self.col=col self.matlist=[] self.create() def create(self): for i in range(self.row): rowlist=[] for j in

我有一个
矩阵
类:

class Matrix(object):
    def __init__(self,row,col):
        self.row=row
        self.col=col
        self.matlist=[]
        self.create()
    def create(self):
        for i in range(self.row):
            rowlist=[]
            for j in range(self.col):
                element=input("Enter Row: {} Col: {} :  ".format(i+1,j+1))
                rowlist.append(element)
            self.matlist.append(rowlist)   

    def __str__(self):
        j=str()
        for i in range(len(self.matlist)):
            for j in range(len(self.matlist[i])):
                j+=str(self.matlist[i][j])+"  "
            j+="\n"
        return j

    def __add__(self,y):
        newmatrix=[]
        el1=self.matlist
        el2=y.matlist

        if (len(el1)==len(el2)) and (len(el1[0])==len(el2[0])):
            for i in range(len(el1)):
                newlist=[]
                for j in range(len(el1[i])):
                    m=el1[i][j]+el2[i][j]
                    newlist.append(m)
                newmatrix.append(newlist)
            return newmatrix
我创建了两个矩阵对象:

m=Matrix(2,2)
n=Matrix(2,2)
然后我加上矩阵:

k=m+n

调用
\uuuu add\uuuu()
方法,该方法返回一个
列表
。但是我希望它以
矩阵
对象的形式返回它,这样当我试图打印它时,
\uuu str\uuu()
方法被调用,而
列表
dosent被简单地打印出来。是否有某种方法可以将
列表
键入
矩阵

类似的方法应该可以

首先,使用classmethod从列表创建矩阵:

class Matrix(object):
    def __init__(self, row=None, col=None):
        self.matlist = []
        if row and col:
            self.row = row
            self.col = col
            self.create()

    @classmethod
    def fromlist(cls, list):
        res = Matrix()
        for row in list:
            res.matlist.append(row)
        return res
然后,
\uuuuuuuuuuuuuuu
方法使用所述方法返回
矩阵

def __add__(self,y):
    newmatrix=[]
    el1=self.matlist
    el2=y.matlist

    if (len(el1)==len(el2)) and (len(el1[0])==len(el2[0])):
        for i in range(len(el1)):
            newlist=[]
            for j in range(len(el1[i])):
                m=el1[i][j]+el2[i][j]
                newlist.append(m)
            newmatrix.append(newlist)
        return Matrix.fromlist(newmatrix)

请注意,您应该进行一些检查。例如,此代码不检查所有行的大小是否相同。在代码中没有考虑的其他检查是,当添加两个<代码>矩阵< /Cord>对象时,它们都应该是相同的大小。

如果你想要一个矩阵,为什么不让方法返回一个?为什么不创建一个额外的构造函数方法来从输入列表中创建一个新的矩阵,并将其用作add方法的返回?@AArias您建议我如何创建一个额外的构造函数?