将矩阵中的元素转换为长字符串。

huangapple go评论53阅读模式
英文:

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']]

huangapple
  • 本文由 发表于 2023年2月14日 05:59:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定