[docs]@singledispatchdefsubset_sequence(x:Any,indices:Sequence[int])->Any:""" Subset ``x`` by ``indices`` to obtain a new object. The default method attempts to use ``x``'s ``__getitem__`` method. Args: x: Any object that supports ``__getitem__`` with an integer sequence. indices: Sequence of non-negative integers specifying the integers of interest. All indices should be less than ``len(x)``. Returns: The result of slicing ``x`` by ``indices``. The exact type depends on what ``x``'s ``__getitem__`` method returns. """returnx[indices]
@subset_sequence.registerdef_subset_sequence_list(x:list,indices:Sequence)->list:returntype(x)(x[i]foriinindices)@subset_sequence.registerdef_subset_sequence_range(x:range,indices:Sequence)->Union[list,range]:ifisinstance(indices,range):# We can just assume that all 'indices' are in [0, len(x)),# so no need to handle out-of-range indices.returnrange(x.start+x.step*indices.start,x.start+x.step*indices.stop,x.step*indices.step)else:return[x[i]foriinindices]