evan.schal...@gmail.com wrote: > I frequently use binary as bool placeholders and find myself filtering > lists based on those bools, this seems to have a similar semantic meaning > as the bit wise ^ or __xor__ operator and could add syntactic sugar to the > base list class. > > Use Case: > > Controlling a stepper at half-step has the following cycle steps: > > CYCLE = [ > [1,0,0,0], > [1,1,0,0], > [0,1,0,0], > [0,1,1,0], > [0,0,1,0], > [0,0,1,1], > [0,0,0,1], > [1,0,0,1], > ] > > which can be represented as follows: > > CYCLE = [ > 1<<3, > 1<<3|1<<2,
[...] > on a raspberrypi, using (for illustration's sake) GPIO pins 1,2,3,4 I'd > like to use the __xor__ method (currently not implemented) to perform the > bit-wise filter as follows: Python isn't C;) A matrix of booleans *can* be expressed using bit twiddling, but I don't see the point. Just the other day I read that raspis now come with up to 8 GB memory. Here's an alternative: >>> import numpy >>> CYCLE = [ ... [1,0,0,0], ... [1,1,0,0], ... [0,1,0,0], ... [0,1,1,0], ... [0,0,1,0], ... [0,0,1,1], ... [0,0,0,1], ... [1,0,0,1], ... ] >>> cycle = numpy.array(CYCLE, dtype=bool) >>> cycle array([[ True, False, False, False], [ True, True, False, False], [False, True, False, False], [False, True, True, False], [False, False, True, False], [False, False, True, True], [False, False, False, True], [ True, False, False, True]], dtype=bool) >>> numpy.where(cycle[1]) (array([0, 1]),) That's zero-based indexing, as it should be; you can convert with >>> numpy.where(cycle[1])[0] + 1 array([1, 2]) or (similar to your list subclass) you can pick arbitrary values: >>> labels = np.array(["first", "second", "third", "fourth"]) >>> labels[cycle[0]] array(['first'], dtype='<U6') >>> labels[cycle[1]] array(['first', 'second'], dtype='<U6') -- https://mail.python.org/mailman/listinfo/python-list