-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path09_list_slicing.py
144 lines (118 loc) · 5.14 KB
/
09_list_slicing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Lists are very useful. In this scripts we will learn how to slice
# lists.
# We already know how to access items in a list by using their
# indexes
fruits = ["apple", "carrot", "banana", "tomato"]
print(fruits[1]) # will print "carrot" since indexes starts at 0
# Sometimes we might need to access items from the end instead of
# from the beginning like we do with indexes.
# Lists in Python support "negative indexing" allowing us to access
# items from the end
print(fruits[-1]) # index -1 refers to the last item
print(fruits[-2]) # index -2 refers to the second last item, etc...
# Slicing lists is also very useful if we need to extract a sublist.
# We do this by specifying from/to indexes
fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
# positive | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
# ---------------------------------------------------------------
# negative | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
fruits_sub_list = fruits[2:5]
print(fruits_sub_list)
# Using slicing we can also get a sublist from the tail using
# negative indexing
print(fruits[-5:-1]) # notice that here "mango" will not be printed
# becuse the "to" index is always exclusive
# If we need a sublist including the last element
print(fruits[-5:])
# To get first the first 5 elements of a list we can do either:
print(fruits[:5])
print(fruits[0:5])
# We can even create a slice of a list containing the whole list.
# Later we will find out why this is useful
print(fruits[:]) # by not specifying from/to we get the whole list
# Can we ask python to return a slice of a list, but only every
# second element? We can:
numbers = list(range(0, 10))
even_from_first_5 = numbers[0:5:2]
print(even_from_first_5)
# numbers[0:5:2] - explanation:
# | | |
# start from the index 0
# | |
# take elements until index 5 exclusive
# |
# take every 2nd (second) item
# The same way we can ask for every 3rd, 4th, ... item.
# The numbers used for slicing are usually named: [from:to:step] or
# [start:stop:step]
# To get every second item from the whole list:
print(numbers[::2])
# We can even ask for items from the end setting step to -1
print(numbers[::-1]) # reversed list
print(numbers[::-2]) # every second item of a reversed list
# Why is this slice empty?
print(numbers[0:3:-1])
# This slice is empty because we set the start to index 0 and the
# step to -1 (going left) but there are no items in the list before
# the 0 element.
# | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | items
# -------------------------------------------------------
# | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | indexes
# start stop
# <- direction
# If we set "start" to the right of "stop" we should get an nonempty
# slice with step -1
print(numbers[7:3:-1])
# | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | items
# -------------------------------------------------------
# | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | indexes
# stop start
# * * * * <- direction
# We can express the same using negative indexing
print(numbers[-2:3:-1])
# | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | items
# -------------------------------------------------------
# | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | indexes pos
# | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 | indexes neg
# stop start
# * * * * <- direction
# Reversing lists is very useful because often we will need to
# process a list from the tail.
things_to_do = ["hoover", "iron", "dishes", "walk", "read book", "code"]
print("wrong order of doing things:")
for x in things_to_do:
print(x)
print("correct order of doing things:")
for x in things_to_do[::-1]:
print(x)
# Slicing lists might be confusing because of the many possibilities
# it provides. While coding use the interactive shell to remind your
# self how slicing works.
# exercise 1: create a list with months.
# a) print it forwards
# b) print it backwards
# c) print sublist of winter months
# d) print sublist of summer months reversed
# e) print months of II and III quoter
# f) print odd months
# g) print even months from march to october
# exercise 2: A coder's work is mostly reading existing code. Read
# the commented code below and before running it, try to deduce what
# will be the output of the code. Write it down, run the code, how
# many list slices did you predict correctly?
cities = ["Gdansk", "Gdynia", "Krakow", "Lodz", "Poznan", "Warsaw", "Wroclaw"]
# print(cities[0:2])
# print(cities[:])
# print(cities[:4])
# print(cities[-5:])
# print(cities[::2])
# print(cities[::-1])
# print(cities[::-3])
# print(cities[0:1:-3])
# print(cities[-1:1:-1])
# print(cities[-1:-9:-2])
# exercise 3: Write a program which will ask the user for a number N.
# The program will compute the sum of all even numbers from
# 0 to N.
# exercise 5: Write a program which will ask the user for a number N
# and print numbers from 1, 2, 3, 4,... until their sum exceeds N