1

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?

6
  • That doesn't even run without specifying dtype. Commented Apr 16, 2019 at 1:12
  • what doesn't run? Commented Apr 16, 2019 at 1:15
  • np.array([1,1,1,[1],1,1,[1],1,1]) gives an error ValueError: setting an array element with a sequence. Commented Apr 16, 2019 at 1:17
  • that's weird, works on my machine. Commented Apr 16, 2019 at 1:18
  • It's a numpy thing; it works for me after updating to 1.16. Commented Apr 16, 2019 at 1:19

1 Answer 1

2

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]),)
Sign up to request clarification or add additional context in comments.

Comments

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.