Normally, if you try to retrieve from an index that’s beyond the bounds of the data structure, you’ll get an error: > s = 'abcdefghij'īut slices are far more forgiving if you go off of the edge of a slice, then Python will simply stop at the edge of your sequence: > s = 'abcdefghij' Just as you can use variables in an index, you can also use them in a slice: > s = 'abcdefghij' > s # Start is smaller, and negative step? Bad news. > s # no indexes? from end to start, backwards > s # from 20 to the beginning, step size 2 > s # from the end to 5 (not including), step size 4 > s # from 20 to 5 (not including), step size 4 Just remember, in such cases, that the start needs to be bigger than the end: > s # from 20 to 5 (not including), step size 1 It gets even better when you discover that the step size can be negative, which allows us to retrieve values in reverse order from the original data structure. > s # from 2, through the end, step size 2 > s # from 2, through the end, step size 4 > s # from start, until (not including) 25, step size 4 > s # from 2, until (not including) 25, step size 4 > s # from 5, until (not including) 20, step size 3 With the combination of start-stop-stride, we can work with all sorts of combinations of things: > import string But by adding another number after a second colon, we can modify this, too: > mylist = But what if we only want every other element? Then we can use the (optional) third part of slice syntax, known as either the “step size” or the “stride.” By default, the step size is 1. Note that this isn’t a perfect solution if you’re copying complex data structures, then you’ll probably want to look at the “copy” module, and explore its “copy” and “deepcopy” methods.īy default, a slice uses each element from the sequence on which it’s working. > biglist =, mylist] # put copies, not mylist, in biglist By contrast, if I use slices, I can avoid this: > mylist = In the above code, changing “mylist” affected “biglist”, because both names were still pointing (one directly, one indirectly) to the original list. > s # not the same as s - doesn't include the endīecause slices create new objects, you can sometimes use them to avoid problems with mutable data: > mylist = > s # from index 5 until (and through) the end > s # from the start until (and not including) index 5 Slice syntax is more flexible than this: You can leave off the starting index, ending index, or both to indicate that you want to go all the way to the extreme edge: > s > mylist * 2 Multiply the slice (list) at mylist by 2 > mylist * 2 # Multiply the element at mylist by 2 Thus, slicing a string returns a string, while slicing a list returns a list and slicing a tuple returns a tuple. Remember that a slice from type X will always return a new object of type X. Rather, I need to run my “for” loop over a complete string… which a slice just happens to return to me: for one_letter in s: # s returns a new string For example, if I’m interested in iterating over letters 3-7 in my string “s” from above, I won’t (and can’t) use a C-like “for” loop, starting the loop at index 3 and ending at index 7. My students often ask me how, without indexes, they can iterate over only part of a string (or other sequence). While “for” loops in Python don’t explicitly use numeric indexes, slices do. Which means that you can have a slice of a string, list, or tuple, and you’ll have the same effect: > mylist = Sure enough, we get the string ‘defgh’ back. We’ve then asked for the string to be returned, starting at index 3 and up to (and not including) index 8. In the above code, we’ve defined a string. The best way to do so is with a “slice”: > s = 'abcdefghij' Let’s say that you have a Python string, and want to grab a substring from it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |