Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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/2/ssis/2.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
__在Python2.7中添加矩阵方法_Python_Oop_Python 2.7_Matrix_Add - Fatal编程技术网

__在Python2.7中添加矩阵方法

__在Python2.7中添加矩阵方法,python,oop,python-2.7,matrix,add,Python,Oop,Python 2.7,Matrix,Add,我是Python新手,所以我需要你的帮助。 程序必须增加和减少随机矩阵 import random class Matrix: def __init__(self): self.mat = [[]] def gen_ran_numb(self,row=5,col=5): self.mat=[[random.randint(0,10) for z in xrange(col)] for z in xrange(row)] def print_

我是Python新手,所以我需要你的帮助。 程序必须增加和减少随机矩阵

import random
class Matrix:
    def __init__(self):
        self.mat = [[]]
    def gen_ran_numb(self,row=5,col=5):
        self.mat=[[random.randint(0,10) for z in xrange(col)] for z in xrange(row)]
    def print_matrix(self):
        print self.mat
    def __add__(self,b):
        mat=[]
        for j in range(len(self.mat)):
            temp=[]            
            for k in range(len(self.mat[0])):
                x=self.mat[j][k] + b.mat[j][k]
                temp.append(x)
            mat.append(temp)
            rez=mat
        return rez
    def __sub__(self,b):
        mat=[]
        for j in range(len(self.mat)):
            temp=[]            
            for k in range(len(self.mat)):
                x=self.mat[j][k] - b.mat[j][k]
                temp.append(x)
            mat.append(temp)            
        return mat        

a=Matrix()
b=Matrix()
c=Matrix()
a.print_matrix()
a.gen_ran_numb(5,5)
b.gen_ran_numb(5,5)
c.gen_ran_numb(5,5)
a.print_matrix()
b.print_matrix()
c.print_matrix()
print b+a
print b+a+c
如果我添加2个矩阵,效果很好,但如果我添加3或4个矩阵,我会出现以下错误:

Traceback (most recent call last):
File "C:/Users/Вадик/Documents/Python/task.py", line 40, in <module>
print b+a+c
TypeError: can only concatenate list (not "instance") to list
回溯(最近一次呼叫最后一次):
文件“C:/Users/Бааааааааааааа1072
打印b+a+c
TypeError:只能将列表(而不是“实例”)连接到列表
我不明白我做错了什么。 请帮帮我。
谢谢大家!

问题是您返回的不是
矩阵
对象,而是实际的矩阵,即列表的列表。所以,当你连接2个对象时,这是可以的,但是当你连接3个对象时,你实际上是在尝试连接一个列表对象和一个
矩阵
对象

换句话说,只需更改函数以返回新实例,如下所示:

def __add__(self, b):
    res = Matrix()
    res.mat = [] #to avoid an unwanted empty list at the beginning of new matrix
    for j in range(len(self.mat)):
        temp = []            
        for k in range(len(self.mat[j])):
            x = self.mat[j][k] + b.mat[j][k]
            temp.append(x)
        res.mat.append(temp)
    return res

您可能也想同样地更改
\uuuu sub\uuuu

您没有返回
矩阵
。您认为应该在add的嵌套循环中写入。。对于范围内的k(len(self.mat[j]):。。。如果行和列不相等..错误很明显<代码>你不能添加矩阵我像你一样编辑代码,但我得到了错误:@user3675561这不是错误。这是新矩阵对象的字符串表示形式。像任何其他对象一样使用它(例如,调用
.print\u matrix()
来打印它)