Hello everyone !
I’m a total beginner in python, and i’m still familiarizing myself with the python syntax, especially with the generation in list, especially when I have to use several of them.
Here’s where i’m stuck :
I’m creating a function that treats matrix that are fed to the function this way:
"""
1 2 3
4 5 6
7 8 9
"""
as you can see, the matrix is given with embedded newlines and the numbers separeted by spaces. To have a simpler time, I tried to seperate the matric in rows, and for this I did the following :
def myfunc(matrix):
formated_matrix = [b for b in matrix.split("\n")]
formated_matrix2 = [a.split(" ") for a in self.test]
given the result [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]
But I was wondering if it was possible to do it in one line, something like
[a for b in matrix_string.split("\n") for a in b.split(" ")]
but it gives the result ['1', '2', '3', '4', '5', '6', '7', '8', '9']
and I cannot figure out how to get the same result as previously stated.
Thanks a lot fot your help !