0

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

2
  • 1
    Did you look at np.argwhere(points2 == p1)? Does that match the np.delete requirements? Practice using np.delete with simple numbers, so you have a clear idea of what it does. In other words, take small steps, making sure each does what you want, and you understand why. Commented Nov 20, 2020 at 7:24
  • Try to select all the rows to remove first, then do just one delete. Repeated deletes in a loop will be slower. Commented Nov 20, 2020 at 7:46

1 Answer 1

1

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],
                    ...
   
              ]
Sign up to request clarification or add additional context in comments.

1 Comment

@ Ziming Shan thanks a lot for your great help. your code worked perfectly

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.