3

I want delete the array from list of arrays in that way

i = np.array([1,2])
facets = [np.array([1,2]),np.array([3,4])]

I want remove an element

facets.remove(np.array(i[0],i[1]))

but obtain an error:

ValueError                                Traceback (most recent call last)
<ipython-input-131-c0d040653e23> in <module>()
----> 1 facets.remove([i[0],i[2]])

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

Is there a way to solve that problem?

5
  • The declaration of facets throws an error. Commented Feb 12, 2016 at 21:42
  • yep=) a mistake=) I wanted to simplify code example. Commented Feb 12, 2016 at 21:48
  • You're code still doesn't parse. The last line needs to be facets.remove(np.array([i[0],i[1]])). Commented Feb 12, 2016 at 21:55
  • I've edited it now, sorry. That doesn't matter for the problem -- in common I have a long list of arrays of size 2, and I wanna remove an array, using another array Commented Feb 12, 2016 at 21:59
  • Right, but we can't reproduce your error quickly or test solutions if your code doesn't work... Commented Feb 12, 2016 at 22:01

3 Answers 3

4

Consider the following example:

ls = [1, 2, 3, 1]
ls.remove(1)

This code does something like:

  1. Iterate through the elements of ls.
  2. Check whether each element is equal to 1.
  3. If it does, it pops that element, and breaks iteration.

In step 2, your code is trying to compare two numpy arrays like array1 == array2. The problem is that numpy returns an array of truth values for this comparison.

>>> np.array([1,2]) == np.array([1,3])
array([ True, False], dtype=bool)

So, you're going to have to implement your own remove-like method.

def remove_from_array(base_array, test_array):
    for index in range(len(base_array)):
        if np.array_equal(base_array[index], test_array):
            base_array.pop(index)
            break
    raise ValueError('remove_from_array(array, x): x not in array')

Usage:

i = np.array([1,2])
facets = [np.array([1,2]),np.array([3,4])]
remove_from_array(facets, i)

print facets # [array([3, 4])]
Sign up to request clarification or add additional context in comments.

Comments

3

The easiest way is to use all() to compare the list element-by-element to the element you want to remove and return all that do not match. Note that this removes all elements of your list that match the array you want to remove.

[ x for x in facets if not (x==i).all()]

8 Comments

But what do I have actually do? (facets == np.array(i[0], i[1])).any() returns False, but facets contains it
I don't get what you mean, just try the code that I've written. It's a list comprehension; it should return a list of arrays.
I mean that I need exactly do that: [ x for x in facets if not (x == [i[0], i[1]]) and I thought it will work with ... (x == np.array(i[0], i[1])
It works, as long as you create your array correctly: [ x for x in facets if not (x==np.array([i[0],i[1]])).all()]
Thank you! I also have the same problem with .index function. Does it mean that it's hard to use built-in functions for some complicated lists?
|
0

Try below :

facets.remove(np.array(i[0],i[1]))

This worked for me as follows :

>>> i = list([1,2])
>>> facets = [list([1,2]),list([3,4])]
>>> facets.remove(list([i[0],i[1]]))
>>> facets
[[3, 4]]

1 Comment

Unfortunately I have a list of np.arrays not a list of lists. That doesn't work if you replace facets = [np.array(1,2), np.array(3,4)]

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.