Home > Software design >  how to remove a tuple based on its elements when the elements are np.array and my code raises value
how to remove a tuple based on its elements when the elements are np.array and my code raises value

Time:03-04

how to remove a tuple based on its elements when the elements are np.array and my code raises value error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I want my code to remove the first tuple and leave the 2nd one how can I do this

tuple_list = [(np.array([1,2]),np.array([3,4])),(np.array([5,6]),np.array([7,8]))]
i = np.array([1,2])
j = np.array([3,4])
filtered_t_l =  [ x for x in tuple_list if (x[0], x[1]) != (i,j) ]

expected output:

[(array([5, 6]), array([7, 8]))]

CodePudding user response:

You need to aggregate to a single boolean, here using any:

filtered_t_l = [x for x in tuple_list if (x[0]!=i).any() or (x[1] != j).any()]

output:

[(array([5, 6]), array([7, 8]))]
  • Related