list comprehension replace for loop in 2D matrix list comprehension replace for loop in 2D matrix python python

list comprehension replace for loop in 2D matrix


You have the order of your loops swapped; they should be ordered in the same way they would be nested, from left to right:

[int(x) for line in data for x in line.split()]

This loops over data first, then for each line iteration, iterates over line.split() to produce x. You then produce one flat list of integers from these.

However, since you are trying to build a list of lists, you need to nest a list comprehension inside another:

Cordi1 = [[int(i) for i in line.split()] for line in data]

Demo:

>>> data = '''\... 2 3 4 5 6 3... 1 2 2 4 5 5... 1 2 2 2 2 4... '''.splitlines()>>> [int(x) for line in data for x in line.split()][2, 3, 4, 5, 6, 3, 1, 2, 2, 4, 5, 5, 1, 2, 2, 2, 2, 4]>>> [[int(i) for i in line.split()] for line in data][[2, 3, 4, 5, 6, 3], [1, 2, 2, 4, 5, 5], [1, 2, 2, 2, 2, 4]]

If you wanted a multidimensional numpy array from this, you can either convert the above directly to an array or create an array from the data then reshape:

>>> import numpy as np>>> np.array([[int(i) for i in line.split()] for line in data])array([[2, 3, 4, 5, 6, 3],       [1, 2, 2, 4, 5, 5],       [1, 2, 2, 2, 2, 4]])>>> np.array([int(i) for line in data for i in line.split()]).reshape((3, 6))array([[2, 3, 4, 5, 6, 3],       [1, 2, 2, 4, 5, 5],       [1, 2, 2, 2, 2, 4]])