Change a matrix to long string in a matrix to elements
Question:
I have the following matrix:
matrix= [[' 0.9111 0.9082 0.9151 0.9023 0.9019 0.9106'],
[' 0.7488 114*0 0.7646 0.7594 0.7533 117*0/'],
[]]
I am trying to convert it to the following form:
[['0.9111', '0.9082', '0.9151', '0.9023', '0.9019', '0.9106'],
['0.7488', '114*0', '0.7646', '0.7594', '0.7533', '117*0']]
I tried different functions like:
list(zip((row.split() for row in matrix)))
updated_matrix = [x.strip() for x in matrix]
Answers:
You need to filter out empty lists and split the 1st item in sublists:
matrix= [[' 0.9111 0.9082 0.9151 0.9023 0.9019 0.9106'], [' 0.7488 1140 0.7646 0.7594 0.7533 1170/'], []]
matrix = [m[0].rstrip('/').split() for m in matrix if m]
print(matrix)
[['0.9111', '0.9082', '0.9151', '0.9023', '0.9019', '0.9106'], ['0.7488', '1140', '0.7646', '0.7594', '0.7533', '1170']]
I have the following matrix:
matrix= [[' 0.9111 0.9082 0.9151 0.9023 0.9019 0.9106'],
[' 0.7488 114*0 0.7646 0.7594 0.7533 117*0/'],
[]]
I am trying to convert it to the following form:
[['0.9111', '0.9082', '0.9151', '0.9023', '0.9019', '0.9106'],
['0.7488', '114*0', '0.7646', '0.7594', '0.7533', '117*0']]
I tried different functions like:
list(zip((row.split() for row in matrix)))
updated_matrix = [x.strip() for x in matrix]
You need to filter out empty lists and split the 1st item in sublists:
matrix= [[' 0.9111 0.9082 0.9151 0.9023 0.9019 0.9106'], [' 0.7488 1140 0.7646 0.7594 0.7533 1170/'], []]
matrix = [m[0].rstrip('/').split() for m in matrix if m]
print(matrix)
[['0.9111', '0.9082', '0.9151', '0.9023', '0.9019', '0.9106'], ['0.7488', '1140', '0.7646', '0.7594', '0.7533', '1170']]