python - NumPy slice notation in a dictionary -
i wonder if possible store numpy slice notation in python dictionary. like:
lookup = {0:[:540], 30:[540:1080], 60:[1080:]} it possible use native python slice syntax, e.g. slice(0,10,2), have not been able store more complex slices. example, multidimensional [:,:2,:, :540].
my current work around store values tuples , unpack these necessary slices.
working in python 2.x.
the syntax [:, :2, :, :540] turned tuple of slice objects python:
(slice(none, none, none), slice(none, 2, none), slice(none, none, none), slice(none, 540, none)) a convenient way generate tuple use special function* np.s_. need pass [...] expression. example:
>>> np.s_[:540] slice(none, 540, none) >>> np.s_[:, :2, :, :540] (slice(none, none, none), slice(none, 2, none), slice(none, none, none), slice(none, 540, none)) then dictionary of slices written as:
lookup = {0: np.s_[:540], 30: np.s_[540:1080], 60: np.s_[1080:]} * technically s_ alias class indexexpression implements special __getitem__ method.
Comments
Post a Comment