[Tutor] Extracting bits from an array

Colin Ross colin.ross.dal at gmail.com
Fri Apr 29 13:15:13 EDT 2016


Hello,

I have an array that takes on the following form:

x = [1000,1001,1011,1111]

The array elements are meant to be binary representation of integers.

Goal: Access array elements and extract the first two bits.

e.g. Final result would look something like this:

x_new = [10,10,10,11]

What I have tried:

data_indices = range(4)      #  Set up array of values to loop over

for idx in data_indices:
     f = x[idx]                          # Index into array of x values
     f_idx = f[:2]                      # Extract first two elements
     print f_idx

I then receive the following error:

IndexError: invalid index to scalar variable.

Any help with accomplishing my outline dgoal would be greatly appreciated.

Thank you.

Colin


More information about the Tutor mailing list