Python-Ref > Data structures > Sequence types in general > Slicing
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Slicing

Retrieving a sub-sequence from a sequence.
Using a notation similar to that of simple Item retrieval it is possible to retrieve slices (sub-sequences) of a sequence. The start and end indexes are separated with a colon.
Please note that the item under the end-index is not part of the slice.
Expand/Shrink
Zdroj: (sequence2-1.py)
  1   a_list = [1,2,3,4,6]                  # list of 5 intergers
  2   a_string = "An example string"        # a string
  3   
  4   
  5   print a_list[0:3]   # first three items
  6   print a_list[1:-1]  # from the second item to the one before last
  7   
  8   print a_string[-6:-1]   # one or both indexes might be negative
stdout:
[1, 2, 3]
[2, 3, 4]
strin
Doba běhu: 21.0 ms
One or both of the indexes might be omitted. Omitting the start-index is the same as using 0, omitting the end-index means "the end of the sequence.
Because the item under the end-index is not part of the slice, omitting the end-index is the most convenient way of slicing to the end of the sequence.
Expand/Shrink
Zdroj: (sequence2-2.py)
  1   a_list = [1,2,3,4,6]                  # list of 5 intergers
  2   a_string = "An example string"        # a string
  3   
  4   
  5   print a_list[:3]   # first three items
  6   
  7   print a_string[-6:]     # from s to the end of the string
  8   print a_string[-6:17]   # this is the same,
  9                           # but we must know the length of the string
 10   
 11   print a_list[:]   # a simple copy of the list
stdout:
[1, 2, 3]
string
string
[1, 2, 3, 4, 6]
Doba běhu: 25.8 ms
If the end-index of a slice is smaller of equal to the start-index (with the exception of negative indexes), the result is an empty sequence. This is even true in case the start-index is out of the range of the length of the sequence.
Expand/Shrink
Zdroj: (sequence2-3.py)
  1   a_list = [1,2,3,4,6]   # list of 5 intergers
  2   
  3   print a_list[1:1]     # empty
  4   print a_list[3:1]     # empty
  5   print a_list[25:20]   # empty, even though the indexes are by far out
stdout:
[]
[]
[]
Doba běhu: 21.2 ms
The slicing has one more option for retrieving sub-sequences from a sequence. This option allows you to specify that you want only some of the items of the slice, for example every second one.
This is specified by adding a second colon character after the start and end indexes and specifying the 'stepping'. This has usually some use only for lists of numbers, it is not so useful for strings.
Expand/Shrink
Zdroj: (sequence2-4.py)
  1   xs = range( 20)  # we use the range function to build a list of integers
  2   
  3   print xs   # the sequence itself
  4   
  5   print xs[0:6]   # normal slicing
  6   print xs[0:6:2]   # only every second item
  7   print xs[::2]   # only even items
stdout:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[0, 1, 2, 3, 4, 5]
[0, 2, 4]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Doba běhu: 21.1 ms