从文本文件中添加两个矩阵(没有模块的Python)

时间:2017-11-26 13:17:15

标签: python file matrix add

我正在尝试从包含以下内容的文本文件中添加两个矩阵:

[[0,1,2],[9,8,7]] [[6,5,4],[3,4,5]]

我有这段代码:

def addition(filename):
a=[]
b=[]
with open(filename, 'r') as myfile:
    data=myfile.read().split()
    a=data[0].split()
    b=data[1].split()
a=a[0]
b=b[0]
print (a)
sum = []
for i in range(len(a)):
    print (i)
    c = []
    for j in range(len(a[0])):
        c.append(a[i][j]+b[i][j])
    sum.append(c)
return sum

但它输出:

[['[['], ['[['], ['06'], [',,'], ['15'], [',,'], ['24'], [']]'], 
[',,'], ['[['], ['93'], [',,'], ['84'], [',,'], ['75'], [']]'], [']]']]

而不是:

[[6,6,6],[12,12,12]]

知道如何解决这个问题吗?谢谢!

0 个答案:

没有答案