What's the use of built-in function slice
and how can I use it?
The direct way of Pythonic slicing I know - l1[start:stop:step]
. I want to know if I have a slice object, then how do I use it?
How it works. First, create a slice object whose start is 0, stop is 4, and step is 2. Second, return a tuple of indices of the slice of the sequence whose length is the length of the colors list. Third, pass the result tuple to the range function to select elements from the colors list.
Python slice() Function The slice() function returns a slice object. A slice object is used to specify how to slice a sequence. You can specify where to start the slicing, and where to end. You can also specify the step, which allows you to e.g. slice only every other item.
Slicing in Python is a feature that enables accessing parts of sequences like strings, tuples, and lists. You can also use them to modify or delete the items of mutable sequences such as lists. Slices can also be applied on third-party objects like NumPy arrays, as well as Pandas series and data frames.
You create a slice by calling slice with the same fields you would use if doing [start:end:step] notation:
sl = slice(0,4)
To use the slice, just pass it as if it were the index into a list or string:
>>> s = "ABCDEFGHIJKL" >>> sl = slice(0,4) >>> print(s[sl]) 'ABCD'
Let's say you have a file of fixed-length text fields. You could define a list of slices to easily extract the values from each "record" in this file.
data = """\ 0010GEORGE JETSON 12345 SPACESHIP ST HOUSTON TX 0020WILE E COYOTE 312 ACME BLVD TUCSON AZ 0030FRED FLINTSTONE 246 GRANITE LANE BEDROCK CA 0040JONNY QUEST 31416 SCIENCE AVE PALO ALTO CA""".splitlines() fieldslices = [slice(*fielddef) for fielddef in [ (0,4), (4, 21), (21,42), (42,56), (56,58), ]] fields = "id name address city state".split() for rec in data: for field,sl in zip(fields, fieldslices): print("{} : {}".format(field, rec[sl])) print('') # or this same code using itemgetter, to make a function that # extracts all slices from a string into a tuple of values import operator rec_reader = operator.itemgetter(*fieldslices) for rec in data: for field, field_value in zip(fields, rec_reader(rec)): print("{} : {}".format(field, field_value)) print('')
Prints:
id : 0010 name : GEORGE JETSON address : 12345 SPACESHIP ST city : HOUSTON state : TX id : 0020 name : WILE E COYOTE address : 312 ACME BLVD city : TUCSON state : AZ id : 0030 name : FRED FLINTSTONE address : 246 GRANITE LANE city : BEDROCK state : CA id : 0040 name : JONNY QUEST address : 31416 SCIENCE AVE city : PALO ALTO state : CA
Square brackets following a sequence denote either indexing or slicing depending on what's inside the brackets:
>>> "Python rocks"[1] # index 'y' >>> "Python rocks"[1:10:2] # slice 'yhnrc'
Both of these cases are handled by the __getitem__()
method of the sequence (or __setitem__()
if on the left of an equals sign.) The index or slice is passed to the methods as a single argument, and the way Python does this is by converting the slice notation, (1:10:2
, in this case) to a slice object: slice(1,10,2)
.
So if you are defining your own sequence-like class or overriding the __getitem__
or __setitem__
or __delitem__
methods of another class, you need to test the index argument to determine if it is an int
or a slice
, and process accordingly:
def __getitem__(self, index): if isinstance(index, int): ... # process index as an integer elif isinstance(index, slice): start, stop, step = index.indices(len(self)) # index is a slice ... # process slice else: raise TypeError("index must be int or slice")
A slice
object has three attributes: start
, stop
and step
, and one method: indices
, which takes a single argument, the length of the object, and returns a 3-tuple: (start, stop, step)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With