Binary numpy array to list of integers?

前端 未结 5 1362
挽巷
挽巷 2020-11-27 07:08

I have a binary array, and I would like to convert it into a list of integers, where each int is a row of the array.

For example:

from numpy import *         


        
5条回答
  •  失恋的感觉
    2020-11-27 08:03

    You could also do this within numpy directly:

    from numpy import *
    a = array([[1, 1, 0, 0], [0, 1, 0, 0], [0, 1, 1, 1], [1, 1, 1, 1]])
    
    b2i = 2**arange(a.shape[0]-1, -1, -1)
    
    result = (a*b2i).sum(axis=1)  #[12  4  7 15]
    

提交回复
热议问题