英文:
Change a matrix to long string in a matrix to elements
问题
我有以下矩阵:
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/'],
[]]
我正在尝试将其转换为以下形式:
[['0.9111', '0.9082', '0.9151', '0.9023', '0.9019', '0.9106'],
['0.7488', '114*0', '0.7646', '0.7594', '0.7533', '117*0']]
我尝试了不同的函数,比如:
list(zip((row.split() for row in matrix)))
updated_matrix = [x.strip() for x in matrix]
如果你需要任何进一步的帮助,请随时告诉我。
英文:
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]
答案1
得分: 0
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']]
英文:
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']]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论