4

What I have is a list where the elements are an array like this:

([1,2,3],[4,5,6],[7,8,9])

What I want is to find the index of an element in this list, something like:

list.index([4,5,6]) #should return 1.

Problem is numpy array comparison throws up errors unless you put something like: (A==B).all()

But this comparison is inside the index function so i can't and don't really want to add the all() bit to the function. Is there an easier solution to this?

5
  • Make the question clearer. Which things are Python lists, and which are numpy arrays? They are not the same thing. Don't mix-n-match. And this thing that you are looking for - will it always be a sublist or row (of an array)? Commented Oct 24, 2015 at 5:59
  • 1
    I answered this question a few months ago, with some time tests: stackoverflow.com/a/25944486/901925 Commented Oct 24, 2015 at 6:15
  • i have had a look at that but for some reason, it doesn't work. the error i am getting is ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(). I think this error has to do with the comparisons within the index function. I have tried converting between python lists and numpy arrays (in response to the question earlier, the place to search in is a list of array and the thing to search for is an array. But it doesn't matter which is which because i can convert between them. Problem is I still get that error when i convert all to lists. Commented Oct 24, 2015 at 8:31
  • That error means that one side or both of a==b is an numpy array, so the result is also an array. But that expression occurs where Python expects a scalar boolean. Test your code in small pieces. Commented Oct 24, 2015 at 11:51
  • List index expects a scalar value when it applies an == test. Commented Oct 24, 2015 at 12:22

2 Answers 2

8

Your last error message indicates that you are still mixing lists and arrays. I'll try to recreate the situation:

Make a list of lists. Finding a sublist works just fine:

In [256]: ll=[[1,2,3],[4,5,6],[7,8,9]]
In [257]: ll.index([4,5,6])
Out[257]: 1

Make an array from it - it's 2d.

In [258]: la=np.array(ll)
In [259]: la
Out[259]: 
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

It does not have an index method

In [260]: la.index([4,5,6])
...
AttributeError: 'numpy.ndarray' object has no attribute 'index'

Make it a list - but we get your ValueError:

In [265]: list(la).index([4,5,6])
...
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

That's because list(la) returns a list of arrays, and arrays produce multiple values in == expressions:

In [266]: list(la)
Out[266]: [array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9])]

The correct way to produce a list from an array is tolist, which returns the original ll list of lists:

In [267]: la.tolist().index([4,5,6])
Out[267]: 1
Sign up to request clarification or add additional context in comments.

Comments

1

If you are starting with a numpy array, you can get the result that you want by converting it to a list of lists before using the index() function, e.g.:

import numpy as np

arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
lst = [list(x) for x in arr]
print (lst.index([4,5,6]))

... which gives the expected output 1.

1 Comment

arr.tolist() == lst

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.