I am to manually merge two given matrices contained in a text file without importing any kind of module. It looks like:
[[1,2][3,4]],[[5,6,7],[8,9,10]]
I have this code:
def combine(filename):
with open(filename, 'r') as myfile:
data=myfile.read().split()
a=data[0].split()
b=data[1].split()
a=eval(a[0])
b=eval(b[0])
row_a=len(a)
row_b=len(b)
col_a=len(a[0])
col_b=len(b[0])
concatenated=[]
if row_a==row_b: #build horizontally
for i in range (row_a):
concatenated.append(a[i])
for i in range (row_b):
concatenated.append(b[i])
return concatenated
if col_a==col_b: #build vertically
for i in range (col_a):
concatenated.append(a[i])
for i in range (col_b):
concatenated.append(b[i])
return concatenated
else:
print ("Error")
But it returns:
[[1, 2], [5, 6, 7], [8, 9, 10], [3, 4], [5, 6, 7], [8, 9, 10]]
Instead of:
[[1,2,5,6,7],[3,4,8,9,10]]
Any ideas on how I can make this work? Thank you in advance!