1

I have a list of NumPy arrays:

self.xy_lat_lon_list = [array([3986139.12431615, 3889959.08475953]),
                        array([3987252.31922408, 3889959.08475953]),
                        array([3988365.51413201, 3889959.08475953]),
                        array([3989478.70903994, 3889959.08475953]),
                        array([3990591.90394788, 3889959.08475953]),
                        array([3991705.09885581, 3889959.08475953]),
                        array([3992818.29376374, 3889959.08475953]),....]

I need to append an integer to every array (the same one) for example:

uav_elev = 1900

output:

self.xy_lat_lon_list = [array([3986139.12431615, 3889959.08475953,1900]),
                        array([3987252.31922408, 3889959.08475953,1900]),
                        array([3988365.51413201, 3889959.08475953,1900]),
                        array([3989478.70903994, 3889959.08475953,1900]),
                        array([3990591.90394788, 3889959.08475953,1900]),
                        array([3991705.09885581, 3889959.08475953,1900]),
                        array([3992818.29376374, 3889959.08475953,1900]),....]

I tried to use list comprehension, but got lost somewhere.

And in a regular for loop:

for l in self.xy_lat_lon_list:
    l. # I thought that I will get append or insert here but It's not. 
2
  • 2
    xy_lat_lon_list = [np.append(a, uav_elev) for a in xy_lat_lon_list] Commented Dec 30, 2019 at 20:30
  • Appending a value to an array requires making a new one. And you can't mix floats and integers in an array. Have you tried np.vstack to turn the list into a 2d array? Commented Dec 30, 2019 at 20:30

2 Answers 2

1

making your list:

In [107]: alist = [np.array([3986139.12431615, 3889959.08475953]), 
     ...:                         np.array([3987252.31922408, 3889959.08475953])
     ...: , 
     ...:                         np.array([3988365.51413201, 3889959.08475953])
     ...: , 
     ...:                         np.array([3989478.70903994, 3889959.08475953])
     ...: ] 
     ...:                                                                       
In [108]: alist                                                                 
Out[108]: 
[array([3986139.12431615, 3889959.08475953]),
 array([3987252.31922408, 3889959.08475953]),
 array([3988365.51413201, 3889959.08475953]),
 array([3989478.70903994, 3889959.08475953])]

Adding the value individually to each array:

In [109]: [np.concatenate((arr, [1900]),axis=0) for arr in alist]               
Out[109]: 
[array([3.98613912e+06, 3.88995908e+06, 1.90000000e+03]),
 array([3.98725232e+06, 3.88995908e+06, 1.90000000e+03]),
 array([3.98836551e+06, 3.88995908e+06, 1.90000000e+03]),
 array([3.98947871e+06, 3.88995908e+06, 1.90000000e+03])]

np.append would work here as well. It is just an alternate way of calling concatenate.

Or you could join the arrays into one 2d array (as long as they are the same size):

In [110]: arr = np.vstack(alist)                                                
In [111]: arr                                                                   
Out[111]: 
array([[3986139.12431615, 3889959.08475953],
       [3987252.31922408, 3889959.08475953],
       [3988365.51413201, 3889959.08475953],
       [3989478.70903994, 3889959.08475953]])

and concatenate a vertical column of values:

In [112]: np.concatenate((arr, np.ones((4,1))*1900), axis=1)                    
Out[112]: 
array([[3.98613912e+06, 3.88995908e+06, 1.90000000e+03],
       [3.98725232e+06, 3.88995908e+06, 1.90000000e+03],
       [3.98836551e+06, 3.88995908e+06, 1.90000000e+03],
       [3.98947871e+06, 3.88995908e+06, 1.90000000e+03]])
Sign up to request clarification or add additional context in comments.

Comments

1

The insert function does exactly what you want:

np.insert(self.xy_lat_lon_list, 2, uav_elev, axis=1)

It returns a 2D array though, so if you need to convert it back to a list of 1D arrays just apply the list() function on it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.