With list comprehension, I am able to take a 20x20 block of numbers in string format, and convert it to a list of lists of integers. The numbers are seperated by white space and the lines are seperated by a newline.
grid = [[int(x) for x in line.split()] for line in nums.split('\n')]
However, what I want is to use numpy for its speed. I could use np.asarray() with my intermediate list, but I don't think that is efficient use of numpy.
I also tried using np.fromstring(), but I can't figure out the logic to make it work for a 2D array.
Is there any way to accomplish this task without the use of creating intermediate python lists?
numpycsvloaders,loadtxtandgenfromtxtoperate as you do - read each line and from that make a list of lists. Then at the end create an array from that. We usenp.array(list_of_lists)all the time, at least for small(er) arrays.