-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09_looping_techniques_test.py
43 lines (36 loc) · 1.31 KB
/
09_looping_techniques_test.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
# Demonstrates printing of information in embedded lists or dictionaries.
# Also demonstrates reversed function to reverse the order.
months_2018 = [[2018, 'January'], [2018, 'Febuary'], [2018, 'March'], ]
for x, y in reversed(months_2018):
print(y, x)
# Trying x, y, z here will not work.
for y, x in enumerate(months_2018):
print(y, x)
# Demonstrate typical use of enumerate, in this case starting from 1.
numbered = dict(enumerate(months_2018, 1))
print(numbered)
print()
# Demonstrates use zip function to link two lists together.
questions = ['age', 'sex']
answers = [9, 'M']
for x, y in zip(questions, answers):
print('What is your {0}? It is {1}.'.format(x, y))
# Demonstrates use zip function combine and unpack two lists.
# Repeat of technique covered in 9_list_comprehension.py
q_and_a = list(zip(questions, answers))
print(q_and_a)
# Reverse zip.
questions, answers = zip(*q_and_a)
print(questions)
print(answers)
print()
# Demonstrates practical use of zip to collect values under the same key.
# There are less sophisticated ways to do this but it demonstrates use of the
# setdefault function, which insets a key with value [] in this case when the
# default key is not found.
key = ['a', 'b', 'a']
value = [1, 2, 3]
dict = {}
for k, v in zip(key, value):
dict.setdefault(k, []).append(v)
print(dict)