Aiden Zhao
Aiden Zhao

Reputation: 653

How to check if numpy array contains empty list

Here is a sample code for data

import numpy as np
myList1 = np.array([1,1,1,[1],1,1,[1],1,1])
myList2 = np.array([1,1,1,[],1,1,[],1,1])

To see if elements in myList1 equals to [1] I could do this:

myList1 == [1]

But for myList2, to see if elements in myList2 equals to [] I COULDN'T do this:

myList2 == []

I had to do:

[x == [] for x in myList2]

Is there another way to look for elements in lists that will also handle empty lists? some other function in numpy or python that I could use?

Upvotes: 1

Views: 2050

Answers (1)

hpaulj
hpaulj

Reputation: 231385

An array with a mix of numbers and lists (empty or not) is object dtype. This is practically a list; fast compiled numpy math no longer works. The only practical alternative to a list comprehension is np.frompyfunc.

Write a small function that can distinguish between a number and list and length of list, and apply that to the array. If it returns True for an empty list, then np.where will identify the location

In [41]: myList1 = np.array([1,1,1,[1],1,1,[1],1,1]) 
    ...: myList2 = np.array([1,1,1,[],1,1,[],1,1])                              

Develop a function that returns True for a empty list, False otherwise:

In [42]: len(1)                                                                 
...
TypeError: object of type 'int' has no len()
In [43]: len([])                                                                
Out[43]: 0

In [44]: def foo(item): 
    ...:     try: 
    ...:         return len(item)==0 
    ...:     except TypeError: 
    ...:         pass 
    ...:     return False 
    ...:                                                                        
In [45]: foo([])                                                                
Out[45]: True
In [46]: foo([1])                                                               
Out[46]: False
In [47]: foo(1)                                                                 
Out[47]: False

Apply it to the arrays:

In [48]: f=np.frompyfunc(foo,1,1)                                               
In [49]: f(myList1)                                                             
Out[49]: 
array([False, False, False, False, False, False, False, False, False],
      dtype=object)
In [50]: np.where(f(myList1))                                                   
Out[50]: (array([], dtype=int64),)
In [51]: np.where(f(myList2))                                                   
Out[51]: (array([3, 6]),)

Upvotes: 2

Related Questions