Colin Ross wrote: > Hello, > > I have an array that takes on the following form: > > x = [1000,1001,1011,1111]
But these are actually integers in decimal representation. You could treat them as binary, but I recommend that you use integers in binary representation to avoid confusion: >>> x = [0b1000, 0b1001, 0b1011, 0b1111] >>> x [8, 9, 11, 15] > The array elements are meant to be binary representation of integers. > > Goal: Access array elements and extract the first two bits. Is the number of bits fixed to four? If so you can shift the bits to the right: >>> y = [v>>2 for v in x] >>> y [2, 2, 2, 3] >>> y [2, 2, 2, 3] All that is left to do now is to convert the result to binary for display purposes: >>> for v in y: print "{:02b}".format(v) ... 10 10 10 11 > 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 This works for strings and sequences, but not for numbers. > print f_idx > > I then receive the following error: > > IndexError: invalid index to scalar variable. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor