简体   繁体   中英

Instantiate two 2D numpy arrays from a list of strings

I have a list of lines in the form:

"a, b, c, d, e ... z,"

Where the first x need to be saved as a row in one 2D array and the rest of the line saved as a row in another 2D array.

Now if this was in C/C++ or Java it would be easy and I could do it in a few seconds. But I haven't got 100% used to python yet. But given for example an array like:

['1, 2, 4, 5,', '2, 3, 6, 3,', '1, 1, 7, 6']

and being told that they have to be split 2 of the columns in the first array two in the second array how would I turn that list into the following two numpy arrays:

[[1, 2]
 [2, 3]
 [1, 1]]

and:

[[4, 5]
 [6, 3]
 [7, 6]]

Also for my own understanding why is it I can't/shouldn't go over each element in a nested for loop and overwrite them one by one. I don't understand why when I attempt that the values don't match what is copied.

For reference the code I tried:

    self.inputs=np.zeros((len(data), self.numIn))
    self.outputs=np.zeros((len(data),self.numOut))
    lineIndex=0
    for line in data:
        d=line.split(',')

        for i in range(self.numIn):
            self.inputs[lineIndex][i]=d[i]
            print d[i],
            self.inputs.index()
        for j in range(self.numOut):
            self.inputs[lineIndex][j]=d[self.numIn+j]
        lineIndex+=1

I suppose it may be easier in python/numpy to create one numpy array with all the values then split it into two separate arrays. If this is easier help with doing that would be appreciated. (How nice am I suggesting possible solutions! :P )

I agree with this last bit:

I suppose it may be easier in python/numpy to create one numpy array with all the values then split it into two separate arrays. If this is easier help with doing that would be appreciated. (How nice am I suggesting possible solutions! :P )

You can strip (to remove trailing comma) and split (to break into list of single characters) each string in a list comprehension to get a list of rows

a = ['1, 2, 4, 5,', '2, 3, 6, 3,', '1, 1, 7, 6']
rows = [l.rstrip(',').split(',') for l in a]
rows
#[['1', ' 2', ' 4', ' 5'], ['2', ' 3', ' 6', ' 3'], ['1', ' 1', ' 7', ' 6']]

Then convert it to an array of integers:

arr = np.array(rows, int)

arr
#array([[1, 2, 4, 5],
#       [2, 3, 6, 3],
#       [1, 1, 7, 6]])

To get the two halves:

arr[:, :2] # first two columns
#array([[1, 2],
#       [2, 3],
#       [1, 1]])

arr[:, -2:] # last two columns
#array([[4, 5],
#       [6, 3],
#       [7, 6]])

Or, to return two arrays:

a, b = np.split(arr, arr.shape[1]/2, axis=1)

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