Extract bit from bitarray according to index list in Python?

Viewed 127

I have two bitarray objects (N.B. bitarray != BitArray) which look like

a = bitarray('10010110')
b = bitarray('01001110')

I want a new bitarray "c" which contains the elements of a at indexes where elements of b are equal to 1. So that

>> c = 0011

I know that doing

b.search(bitarray('1')) 

returns the list of indexes I am interested in but I cannot find a way to combine the bitarray a with the index list.

I know that a solution is to convert a to a list, use np.take to extract a list according to the index list and convert the result to bitarray. Nevertheless, this solution is too much demanding (memory and computational). My full code comprehends a kind of iteration of this operation and a test with a 1e6 array took about 10s which is way too much. I'm supposed to work with very long array (5e9) which is the reason why I chosen the bitarray library. As far as I understood, this library is the most suitable in term of memory optimization.

Here it is my "list" solution

indexList=b.search(bitarray('1'))
c=bitarray((np.take(a.tolist(),indexList)).tolist())
0 Answers
Related