yihao ren
yihao ren

Reputation: 379

Remove array from 2D array in for loop

I have a 2D array that is x y z coordinates.

array([[ 1. ,  1. ,  1. ],
       [ 2. ,  3. ,  1. ],
       [ 3. ,  4. ,  1. ],
       [ 3.1,  5.1,  1. ],
       [ 3.2,  5.2,  1. ],
       [ 3.4,  5.3,  1. ],
       [ 3.5,  5.4,  1. ],
       [ 3.7,  5.5,  1. ],
       [ 3.8,  5.6,  1. ],
       [ 3.9,  5.7,  1. ],
       [ 4. ,  5.8,  1. ],
       [ 4.1,  5.9,  1. ],
       [ 4.2,  6. ,  1. ],
       [ 5. ,  6.1,  1. ],
       [ 9. ,  6.2,  1. ],
       [10. , 10. ,  1. ]])

I want to delete one subarray at a time as I loop through the 2D array. So that in the first loop the first subarray will be deleted and the 2D array will be

array([[ 2. ,  3. ,  1. ],
       [ 3. ,  4. ,  1. ],
       [ 3.1,  5.1,  1. ],
       [ 3.2,  5.2,  1. ],
       [ 3.4,  5.3,  1. ],
       [ 3.5,  5.4,  1. ],
       [ 3.7,  5.5,  1. ],
       [ 3.8,  5.6,  1. ],
       [ 3.9,  5.7,  1. ],
       [ 4. ,  5.8,  1. ],
       [ 4.1,  5.9,  1. ],
       [ 4.2,  6. ,  1. ],
       [ 5. ,  6.1,  1. ],
       [ 9. ,  6.2,  1. ],
       [10. , 10. ,  1. ]])

In the second loop, the second subarray will be deleted and the 2D array will be

array([[ 1. ,  1. ,  1. ],
       [ 3. ,  4. ,  1. ],
       [ 3.1,  5.1,  1. ],
       [ 3.2,  5.2,  1. ],
       [ 3.4,  5.3,  1. ],
       [ 3.5,  5.4,  1. ],
       [ 3.7,  5.5,  1. ],
       [ 3.8,  5.6,  1. ],
       [ 3.9,  5.7,  1. ],
       [ 4. ,  5.8,  1. ],
       [ 4.1,  5.9,  1. ],
       [ 4.2,  6. ,  1. ],
       [ 5. ,  6.1,  1. ],
       [ 9. ,  6.2,  1. ],
       [10. , 10. ,  1. ]])

and so on.

Currently what I have is the following:

xline =[1, 2, 3, 3.1, 3.2, 3.4, 3.5, 3.7, 3.8, 3.9, 4, 4.1, 4.2, 5, 9, 10]
yline =[1, 3, 4, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6,5.7, 5.8, 5.9, 6, 6.1, 6.2, 10]
zline =[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

df = DataFrame(xline,columns=['x_value'])
df['y_value']=yline
df['z_value']=zline

points=df.values

for p1 in points:
    points2=points[:]
    points3=np.delete(points2, np.argwhere(points2 == p1))
    print(points3)

But my code seems to reduce the 2D array to a 1D array. Does anyone know how to fix this?

Thanks a lot for your support and help.

Sincerely

Wilson

Upvotes: 0

Views: 378

Answers (1)

Ziming Shan
Ziming Shan

Reputation: 146

This can be done using list indexing and concatenation. I'll use a different (shorter) list here to demonstrate because otherwise it'd be too long.

Suppose we have a list

L = [1,2,3,4,5,6,7]

and we want to do exactly what you asked. Then we can do

for i in range(0, len(L)):
    new_L = L[0:i] + L[i+1:len(L)]
    print(new_L)    

Output:

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

Instead of deleting from the list, you can create a new list without the ith element. The same code should also work for numpy arrays.

Sidenote: If you are just looking to create a coordinates list given xline, yline, and zline, it's probably much easier to use Python's list comprehension:

coords = [[x,y,z] for x,y,z in zip(xline,yline,zline)]

This will return a list in the form of

coords =     [  [x1, y1, z1],
                [x2, y2, z2],
                [x3, y2, z3],
                    ...
   
              ]

Upvotes: 1

Related Questions