Understanding memory allocation for large integers in Python

Published on

Python uses C structs to represent objects, includingints: struct _longobject { PyObject_VAR_HEAD digit ob_digit[1]; }; PyObject_VAR_HEAD is just a macro that adds other fields in the struct and, ob_digits is an array holding the value for the number. Boiler-plate in size comes from that struct, for small and large Python numbers. Why increments of 4 … Continue reading Understanding memory allocation for large integers in Python

What’s the difference between a reversed tuple and a reversed list?

Published on

Basically, a list implements the __reversed__ method and returns an specialized object, while tuple falls back to the default implementation of reversed for any sequence: >>> list.__reversed__ <method ‘__reversed__’ of ‘list’ objects> >>> tuple.__reversed__ AttributeError: type object ‘tuple’ has no attribute ‘__reversed__’ Now, why list does not default to the sequence reversed object has to … Continue reading What’s the difference between a reversed tuple and a reversed list?

Removing elements from an array that are in another array

Published on

Say I have these 2D arrays A and B. How can I remove elements from A that are in B. A=np.asarray([[1,1,1], [1,1,2], [1,1,3], [1,1,4]]) B=np.asarray([[0,0,0], [1,0,2], [1,0,3], [1,0,4], [1,1,0], [1,1,1], [1,1,4]]) #output = [[1,1,2], [1,1,3]] Here is a Numpythonic approach with broadcasting: In [83]: A[np.all(np.any((A-B[:, None]), axis=2), axis=0)] Out[83]: array([[1, 1, 2], [1, 1, 3]]) … Continue reading Removing elements from an array that are in another array