Binary Sort on Python List __xor__

Peter Otten __peter__ at web.de
Sun May 31 12:44:40 EDT 2020


Evan Schalton wrote:

> Peter,
> 
> This isn't a ram consideration as much it's a logical consideration. There
> are a lot of ways to handle this, I REALLY don't want to use a package
> here. Bit masking is incredibly useful for permutations/combinatoric
> algorithms. I can create my own class wrapper or functions, and optimize,
> but think that the __and__ operator would be a really useful syntactic
> tool. It would streamline these types of operations and *hopefully* help
> people write more efficient code.
> 
> I'm less strictly interested in the & operator explicitly working with a
> bit int, but think it'd be great if the was a built-in filter something
> like:
> 
> [1,2,3,4] & [0,0,1,1] => [3,4] OR
> [1,2,3,4] & [False, False, True, True] = [3,4]

But, but, but...
for numpy arrays this is done regularly, and the established way to spell it 
is

>>> a = np.array([1,2,3,4])
>>> b = np.array([False, False, True, True])
>>> a[b]
array([3, 4])

rather than a & b

whereas for lists IMO it's a fringe application.
I may be influenced by numpy's example, but I'd expect

[1, 2, 3, 4] & [0, 0, 1, 1] --> [0, 0, 1, 0]

i. e. bitwise per-element and.



More information about the Python-list mailing list