简体   繁体   中英

Python and creating 2d numpy arrays from list

I'm running into some trouble converting values in columns into a Numpy 2d array. What I have as an output from my code is something the following:

38617.0 0 0
40728.0 0 1
40538.0 0 2
40500.5 0 3
40214.0 0 4
40545.0 0 5
40352.5 0 6
40222.5 0 7
40008.0 0 8
40017.0 0 9
40126.0 0 10
40029.0 0 11
39681.5 0 12
39973.0 0 13
39903.0 0 14
39766.5 0 15
39784.0 0 16
39528.5 0 17
39513.5 0 18

And this continues for ~300,000 lines. The coords of the data are arranged as (z,x,y), and I want to convert it into a 2d array with dimensions 765X510 (x,y) so that the z-coordinates are sitting at their respective (x,y) coordinates so that I may write it to an image file.

Any ideas? I've been looking around and I haven't found anything on the matter.


EDIT:

This is the while-loop that's creating the above columns of data (it's actually two, a function is called within another while-loop):

def make_median_image(x,y):
        while y < 509:
                y = y + 1 # Makes the first value (x,0), b/c Python is indexed at 0
                median_first_row0 = sc.median([a11[y,x],a22[y,x],a33[y,x],a44[y,x],a55[y,x],a66[y,x],a77[y,x],a88[y,x],a99[y,x],a1010[y,x]])
                print median_first_row0,x,y
                list1 = [median_first_row0,x,y]
                list = list1.append(

while x < 764:
        x = x + 1
        make_median_image(x,y)
import numpy as np
l = [[1,2,3], [4,5,6], [7,8,9], [0,0,0]]

You can directly pass a python 2D list into a numpy array.

>>> np.array(l)
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9],
       [0, 0, 0]])

If you only want the latter two columns (which are your x,y values)

>>> np.array([[i[1],i[2]] for i in l])
array([[2, 3],
       [5, 6],
       [8, 9],
       [0, 0]])

Would it be possible to create an array from the data below like so?

Data:

38617.0 0 0
40728.0 0 1
40538.0 0 2
40500.5 0 3
40214.0 0 4
40545.0 0 5
40352.5 0 6
40222.5 0 7
40008.0 0 8
40017.0 0 9
40126.0 0 10
40029.0 0 11
39681.5 0 12
39973.0 0 13
39903.0 0 14
39766.5 0 15
39784.0 0 16
39528.5 0 17
39513.5 0 18
... (continues for ~100,000 lines, so you can guess why I'm adamant to find an answer)

What I would like:

numpy_ndarray = [[38617.0, 40728.0, 40538.0, 40500.5, 40214.0, 40545.0, 40352.5, ... (continues until the last column value in the data above is 764) ], [begin next line, when x = 1, ... (until last y-value is 764)], ... [ ... (some last pixel value)]]

So it basically builds a matrix/image grid out of the pixel values in the first column of data that's associated with the (x,y) coordinate in the second and third columns.

lets say your array is l

import numpy as np
l = [[1,2,3], [4,5,6], [7,8,9], [0,0,0]]

>>> np.array(l)
array([[1, 2, 3],
   [4, 5, 6],
   [7, 8, 9],
   [0, 0, 0]])

this should work

>>> np.array([[i[1] for i in l],[i[2] for i in l]])
array([[2, 5, 8, 0],
   [3, 6, 9, 0]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM