Up: Python

Slicing

slide 001 Hello, and welcome to the twelfth episode of the Software Carpentry lecture on Python. This episode will show you how to take sections out of lists, strings, and tuples.
slide 002 Lists, strings, and tuples are all sequences.
slide 003 Which means they can all be indexed by integers in the range 0 to len minus 1.
slide 004 But they can also be sliced using a range of indices.
slide 005 To see how slicing works, let’s assign the string 'uranium' to the variable element.
slide 006 If we index the string with the expression 1:4, we get back the characters from index 1, up to but not including index 4, i.e., characters 1, 2, and 3.
slide 007 If we don’t specify the lower bound, it defaults to 0, which is the start of the string.
slide 008 Similarly, if we don’t specify the upper bound, it defaults to the end of the string.
slide 009 And we can use negative indices as bounds too, which count positions backward from the end of the string. The slice expression -4: gives us characaters -4, -3, -2, and -1.
slide 010 Indices are interpreted the same way for slices as they are for single elements, but there is one important difference. When a single index is used to get one element from a string or list, Python always checks that it’s in bounds, and gives an error if it isn’t.
slide 011 But Python just truncates out-of-bounds values when slicing.
slide 012 Here’s our string 'uranium' again.
slide 013 If we try to get element 400, we get an error, because the string isn’t that long.
slide 014 If we take a slice from index 1 up to index 400, though, Python rounds down the upper bound for us.
slide 015 Some people find this useful…
slide 016 …but others trip over the inconsistency from time to time.
slide 017 This behavior is handy, though: no matter how long the string text is, the expression text[1:3] is always legal, but its value may be zero, one, or two characters long.
slide 018 Have a look at these examples and make sure you understand why each one has the value it does.
slide 019 To be consistent, the expression text[1:1] is always the empty string.
slide 020 Because going from location 1, up to but not including location 1, is an empty range.
slide 021 Carrying on, if the lower bound is greater than the upper bound, that’s an empty string too.
slide 022 Not the reverse of the string that would be selected if the bounds were reversed.
slide 023 However, when we compare bounds, we have to remember that negative indices count backward: text[1:-1] is everything except the first and last characters of the string, because the index -1 means “the next-to-last legal index of the sequence”. It may look odd, but it’s consistent.
slide 024 The other thing that’s important to remember about slicing is that it always creates a new object.
slide 025 But only the thing being sliced is copied, so aliasing is still possible.
slide 026 Here’s an example: the list points has four element, each of which is a reference to a two-element list.
slide 027 The expression points[1:-1] creates a new list with two elements. Those two elements are references to the second and third of the sublists that points referred to.
slide 028 If we change the content of those sublists…
slide 029 …by reaching through middle
slide 030 …then not only is middle changed…
slide 031 …but points appears to change as well.
slide 032 Let’s have another look at what just happened. Here’s how points is laid out in memory.
slide 033 When we slice it to create middle, Python copies the second and third elements of points, but those values are references to other lists. points and middle now both contain references to those sublists.
slide 034 If we overwrite middle[0][0]
slide 035 …and middle[1][10], the changes are shared by points.
slide 036

  1. No comments yet.
  1. No trackbacks yet.