Binary numpy array to list of integers?

前端 未结 5 1361
挽巷
挽巷 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:11

    Another one:

    def row_bits2int(arr):
        n = arr.shape[1]  # number of columns
    
        # shift the bits of the first column to the left by n - 1
        a = arr[:, 0] << n - 1  
    
        for j in range(1, n):
            # "overlay" with the shifted bits of the next column
            a |= arr[:, j] << n - 1 - j  
        return a
    

提交回复
热议问题