2

I have a python array of 3d points such as [p1,p2,...,pn] where p1 = [x1,y1,zi] I want to check weather a particular point p_i is a member of this, what is the right method for this?

Here is the code which I tried

import numpy
my_list = []
for x in range(0,10):
    for y in range(0,10):
        for z  in range(0,5):
            p1 = numpy.array([x,y,z])
            my_list.append(p1)

check_list = numpy.array([[1,2,3],[20,0,20],[5,5,5]])
for p in check_list :
    if p not in my_list:
        print (p)

However I;m getting the error

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Apparently this method works for strings and numbers but not for arrays. What is the correct way to do this?

0

3 Answers 3

1
import numpy

my_list = []
for x in range(0, 10):
    for y in range(0, 10):
        for z in range(0, 5):
            p1 = numpy.array([x, y, z])
            my_list.append(p1)

check_list = numpy.array([[1, 2, 3], [20, 0, 20], [5, 5, 5]])
for p in check_list:
    if not numpy.any(numpy.all(p == my_list, axis=1)):
        print(p)
Sign up to request clarification or add additional context in comments.

Comments

1

Try using all along axis=1 and check if any of the result is True:

for p in check_list:
    if not (p==my_list).all(1).any():
        print(p)

Output:

[20  0 20]
[5 5 5]

Comments

1

Give the below a try

from dataclasses import dataclass
from typing import List


@dataclass
class Point:
    x: int
    y: int
    z: int


points: List[Point] = []
for x in range(0, 10):
    for y in range(0, 10):
        for z in range(0, 5):
            points.append(Point(x, y, z))
print(Point(1, 2, 3) in points)
print(Point(1, 2, 3888) in points)

2 Comments

Thank you for the suggestion, can you please tell me what exactly this one is doing? . points: List[Point] = [] ?
@brownfox I can tell you for sure :-). List[Point] is type hint. It increase the readability of the code and is used by type checkers. see docs.python.org/3/library/typing.html BTW - I think this solution is cleaner - it does not require any external lib!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.