3. Slicing

3.1. Accessing element with index

  • Index must be positive or negative int

  • Index must be less or equal to length of object

  • Negative index starts from the end and go right to left

3.1.1. Accessing element from start

text = 'We choose to go to the Moon!'

text[0]         # 'W'
text[1]         # 'e'
text[23]        # 'M'

3.1.2. Accessing element from back

text = 'We choose to go to the Moon!'

text[-1]        # '!'
text[-5]        # 'M'

3.1.3. Accessing not existing element

text = 'We choose to go to the Moon!'

text[100]
# IndexError: string index out of range

3.2. Accessing range of elements

  • Slice has three indexes

    • start (inclusive)

    • stop (exclusive)

    • step

  • Slice Index must be positive or negative int

  • Negative index starts from the end and go right to left

3.2.1. Accessing slice from start

text = 'We choose to go to the Moon!'

text[0:2]       # 'We'
text[:2]        # 'We'
text[3:9]       # 'choose'
text[23:28]     # 'Moon!'

3.2.2. Accessing slice from back

text = 'We choose to go to the Moon!'

text[-5:]       # 'Moon!'
text[-5:-1]     # 'Moon'
text[:-6]       # 'We choose to go to the'
text = 'We choose to go to the Moon!'

text[4:-2]  # 'hoose to go to the Moo'
text[-5:5]  # ''

3.2.3. Accessing slice not existing elements

text = 'We choose to go to the Moon!'

text[:100]  # 'We choose to go to the Moon!'
text[100:]  # ''

3.2.4. Accessing slice from all elements

text = 'We choose to go to the Moon!'

text[:]               # 'We choose to go to the Moon!'

3.2.5. Arithmetic operations on slice indexes

text = 'We choose to go to the Moon!'
first = 23
last = 28

text[first:last]       # 'Moon!'
text[first:last-1]     # 'Moon'

3.2.6. Every n element

text = 'We choose to go to the Moon!'

text[::2]             # 'W hoet ot h on'

3.2.7. Reversing

text = 'We choose to go to the Moon!'

text[::-1]            # '!nooM eht ot og ot esoohc eW'
text[::-2]            # '!oMeto go soce'

3.3. Slice data structures

3.3.1. Slicing str

DATA = 'abcde'

DATA[2]             # 'c'
DATA[-1]            # 'e'
DATA = 'abcde'

DATA[:3]            # 'abc'
DATA[3:]            # 'de'
DATA[1:4]           # 'bcd'
DATA = 'abcde'

DATA[::2]           # 'ace'
DATA[::-1]          # 'edcba'

3.3.2. Slicing tuple

DATA = ('a', 'b', 'c', 'd', 'e')

DATA[2]             # 'c'
DATA[-1]            # 'e'
DATA = ('a', 'b', 'c', 'd', 'e')

DATA[:3]            # ('a', 'b', 'c')
DATA[3:]            # ('d', 'e')
DATA[1:4]           # ('b', 'c', 'd')
DATA = ('a', 'b', 'c', 'd', 'e')

DATA[::2]           # ('a', 'c', 'e')
DATA[::-1]          # ('e', 'd', 'c', 'b', 'a')

3.3.3. Slicing list

  • Slicing works the same as for str

DATA = ['a', 'b', 'c', 'd', 'e']

DATA[1]             # 'b'
DATA[-2]            # 'd'
DATA = ['a', 'b', 'c', 'd', 'e']

DATA[:3]            # ['a', 'b', 'c']
DATA[3:]            # ['d', 'e']
DATA[1:4]           # ['b', 'c', 'd']
DATA = ['a', 'b', 'c', 'd', 'e']

DATA[::2]           # ['a', 'c', 'e']
DATA[::-1]          # ['e', 'd', 'c', 'b', 'a']

3.3.4. Slice dict

DATA = {'a': 1, 'b': 2}

DATA[1:2]
# TypeError: unhashable type: 'slice'

3.3.5. Slice set

  • Slicing set is not possible

DATA = {'a', 'b', 'c', 'd', 'e'}

DATA[1]
# TypeError: 'set' object is not subscriptable
DATA = {'a', 'b', 'c', 'd', 'e'}

DATA[1:2]
# TypeError: 'set' object is not subscriptable

3.4. Slice function

  • Slice object can be returned from function

  • Function can, for example, calculate starting point of a sub-string

text = 'We choose to go to the Moon!'

between = slice(23, 28)
text[between]
# 'Moon!'

3.5. Assignments

3.5.1. Simple collections

  • Filename: slice_every_nth.py

  • Lines of code to write: 5 lines

  • Estimated time of completion: 5 min

  1. Stwórz a: tuple z cyframi 0, 1, 2, 3

  2. Stwórz b: list z cyframi 2, 3, 4, 5

  3. Stwórz c: set, który będzie zawierał co drugie elementy z a i b

  4. Wyświetl c na ekranie

The whys and wherefores
  • Definiowanie i korzystanie z list, tuple, set

  • Slice zbiorów danych

  • Rzutowanie i konwersja typów

3.5.2. Slicing text

  • Filename: slice_text.py

  • Lines of code to write: 8 lines

  • Estimated time of completion: 10 min

  1. Z podanych poniżej ciągów znaków

  2. Za pomocą [...] wydobądź Jana III Sobieskiego

  3. Jakie parametry użyłeś dla każdej z linijek?

a = 'UL. Jana III Sobieskiego 1/2'
b = 'ulica Jana III Sobieskiego 1 apt 2'
c = 'os. Jana III Sobieskiego'
d = 'plac Jana III Sobieskiego 1/2'
e = 'aleja Jana III Sobieskiego'
f = 'alei Jana III Sobieskiego 1/2'
g = 'Jana III Sobieskiego 1 m. 2'
h = 'os. Jana III Sobieskiego 1 apt 2'

expected = 'Jana III Sobieskiego'
print(f'{a == expected}\t a: "{a}"')
print(f'{b == expected}\t b: "{b}"')
print(f'{c == expected}\t c: "{c}"')
print(f'{d == expected}\t d: "{d}"')
print(f'{e == expected}\t e: "{e}"')
print(f'{f == expected}\t f: "{f}"')
print(f'{g == expected}\t g: "{g}"')
print(f'{h == expected}\t h: "{h}"')
The whys and wherefores
  • Definiowanie zmiennych

  • Wycinanie elementów stringów

  • Indeksacja elementów

3.5.3. Iris dataset

  • Filename: slice_iris.py

  • Lines of code to write: 30 lines

  • Estimated time of completion: 20 min

  1. Mając dane z listingu poniżej

    DATA = (
        5.8, 2.7, 5.1, 1.9, 'virginica',
        5.1, 3.5, 1.4, 0.2, 'setosa',
        5.7, 2.8, 4.1, 1.3, 'versicolor',
        6.3, 2.9, 5.6, 1.8, 'virginica',
        6.4, 3.2, 4.5, 1.5, 'versicolor',
        4.7, 3.2, 1.3, 0.2, 'setosa',
    )
    
  2. Za pomocą slice wyodrębnij zmienną features: List[Tuple[float]] z wynikami pomiarów

    features = [
        (5.8, 2.7, 5.1, 1.9),
        (5.1, 3.5, 1.4, 0.2),
        (5.7, 2.8, 4.1, 1.3),
        (6.3, 2.9, 5.6, 1.8),
        (6.4, 3.2, 4.5, 1.5),
        (4.7, 3.2, 1.3, 0.2),
    ]
    
  3. Za pomocą slice (co piąty element) wyodrębnij zmienną labels: List[str], która będzie zawierała w kolejności wszystkie nazwy gatunków:

    labels = [
        'virginica',
        'setosa',
        'versicolor',
        'virginica',
        'versicolor',
        'setosa',
    ]
    
  4. Wyodrębnij zmienną species: Set[str], która jest unikalnym zbiorem gatunków (na podstawie labels)

    species = {
        'versicolor',
        'setosa',
        'virginica',
    }
    
The whys and wherefores
  • Definiowanie i korzystanie z list, tuple, set

  • Slice zbiorów danych

  • Rzutowanie i konwersja typów