-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdictionaries.py
80 lines (57 loc) · 1.75 KB
/
dictionaries.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
slang = {'kicks': 'sneakers', 'whips': 'cars', 'flicks': 'movies'}
print(slang)
# lookup for key value instead of index
print slang['kicks']
# adding values
slang = {}
slang['celly'] = 'telephone'
slang['telly'] = 'hotel'
slang['food box'] = 'belly'
print slang
# updating values
slang['celly'] = 'phone'
print slang['celly']
# removing values
del slang['celly']
print slang
#look ups
#throws error, does not exist
#print slang['bloody']
# returns none, does not exist
r = slang.get('bloody')
if r:
print r
else:
print 'Not found'
# translate using dictionaries
slang = {'mate':'buddy', 'knackered':'tired', 'cheeky':'offensive'}
sentence = 'Sorry my mate is a bit cheeky'
translation = 'Sorry' + ' my ' + slang.get('mate') + ' is a bit ' + slang.get('cheeky')
print translation
# comparing lists & dictionaries
# note list must be ordered the same to match
my_list = [1, 2, 3, 4]
your_list = [1, 3, 2, 4]
her_list = [1, 2, 3, 4]
print (my_list == your_list)
print (my_list == her_list)
# dictionaries don't need to be ordered the same to match
my_list = { 1:1, 2:2, 3:3, 4:4 }
your_list = { 2:2, 3:3, 1:1, 4:4 }
print your_list == my_list
# a list of lists
menus = [['Bacon & Eggs', 'Cereal', 'French Toasts', 'Pancakes'],
['Soup', 'Salad', 'Roast Beef Sandwich', 'Hot Dog'],
['Steak', 'Fish', 'Burger']]
print 'Breakfast:\t', menus[0]
print 'Lunch:\t', menus[1]
print 'Dinner:\t', menus[2]
# getting a value from a 2D list
print 'Favorite Breakfast:\t', menus[0][0]
# a dictionary of lists
menus = {'Breakfast': ['Bacon & Eggs', 'Cereal', 'French Toasts', 'Pancakes'],
'Lunch': ['Soup', 'Salad', 'Roast Beef Sandwich', 'Hot Dog'],
'Dinner': ['Steak', 'Fish', 'Burger']
}
# beast mode
print menus.get('Breakfast')[0]