-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_dictionaries.py
76 lines (69 loc) · 1.75 KB
/
my_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
"""
Learn about dictionaries
"""
from pprint import pprint as pp
def main():
"""
Test function
:return:
"""
urls = {
"google": "www.google.com",
"yahoo": "www.yahoo.com",
"twitter": "www.twitter",
"wsu": "weber.edu"
}
print(urls, type(urls))
# Access by key: [key]
print(urls["wsu"])
# Build dict with dict() constructor
names_age = [('Alice', 32), ('Mario', 23), ('Hugo', 21)]
d = dict(names_age)
print(d)
# Another method
phonetic = dict(a='alpha', b='bravo', c='charlie', d='delta')
print(phonetic)
# make a copy
e = phonetic.copy()
print(e)
# Updating a dictionary: update() method
stocks = {'GOOG':891, 'AAPL':416, 'IBM':194}
print(stocks)
stocks.update({'GOOG':999, 'YHOO':2})
print(stocks)
# Iteration default is by key value
for key in stocks:
print("{k} => {v}".format(k=key, v=stocks[key]))
# Iterate by values
for val in stocks.values():
print("val = ", val)
# Iterate by both key and value with: items()
for items in stocks.items():
print(items)
for key, val in stocks.items():
print(key, val)
# test for membership via key
print('GOOG' in stocks)
print('WINDOWS' not in stocks)
# Deleting: del keyword
print(stocks)
del stocks['YHOO']
print(stocks)
# Mutability of dictionaries
isotopes = {
'H': [1, 2, 3],
'He': [3, 4],
'Li': [6, 7],
'Be': [7, 9, 10],
'B': [10, 11],
'C': [11, 12, 13, 14]
}
print("\n\n\n") # 3 empty lines
pp(isotopes)
isotopes['H'] += [4, 5, 6, 7]
pp(isotopes)
isotopes['N'] = [13, 14, 15]
pp(isotopes)
if __name__ == '__main__':
main()
exit(0)