-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch6e-dict.py
129 lines (105 loc) · 3.44 KB
/
ch6e-dict.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 15 22:10:27 2021
@author: HMFR
"""
#Dictionaries also called hashes or hash tables or associative arrays
#dictionary_name = {key_1: value_1, key_N: value_N}. create an empty dictionary,
# use dictionary_name = {}.
contacts = {'secretary':'5550','sianat':'5512','teleport':'5518','lab':'5521'}
print(contacts['sianat'])
print('dial {} to coordinate for crosspol'.format(contacts['sianat']))
print('-------------------------1')
#change value by key
contacts['sianat']='13728'
print(contacts)
print('-------------------------2')
#adding items to dictionary
contacts['info']='162'
print(contacts)
print('-------------------------3')
#deleting items using del statement
del contacts['lab']
print(contacts)
print('-------------------------4')
#values in the list can be of diff types
contacts['ANT']=['5546','5548']
print(contacts)
print('-------------------------5')
# using 'in' we can access keys
for num in contacts:
print(num)
print('-------------------------5.5')
# using 'in' we can access values for keys
for num in contacts['ANT']:
print(num)
#using keys() function to return keys of a dict
print(contacts.keys())
print('-------------------------6')
#value in dictionaey.keys() return True or False
if 'sianat' in contacts.keys():
print(contacts['sianat'])
print('-------------------------7')
if 'ANT' in contacts.keys():
print(contacts['ANT'][1]) #second element of list
print('-------------------------8')
#using values() func for accessing values in dict
print(contacts.values())
print('-------------------------9')
print('5550' in contacts.values())
print('-------------------------10')
#looping through a dict. good pattern to use plural for dict name like contacts
for contact in contacts:
print('the tel num for {} is {}'.format(contact,contacts[contact]))
print('-------------------------11')
#using items() func to reuten keys and values in pairs
print(contacts.items())
print('-------------------------12')
#looping through a dict. using two variable in for loop using items()
for name, tel in contacts.items():
print(name,tel)
print('-------------------------13')
#nesting dicts
contacts = {}
print(contacts)
contacts = {
'sianat':{
'email':'[email protected]',
'tel':'13728'
},
'teleport':{
'email':'[email protected]',
'tel':'5518'
},
'lab':{
'email':'[email protected]',
'tel':'5521'
}
}
print(contacts)
print('-------------------------14')
for name in contacts:
print('the section is {}'.format(name))
print('its email is {}'.format(contacts[name]['email']))
print('its tel is {}'.format(contacts[name]['tel']))
#using zip() for looping over multiple lists
questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue']
for q, a in zip(questions, answers):
print('What is your {0}? It is {1}.'.format(q, a))
#to loop in reverse use reversed() func
for i in reversed(range(1, 10, 2)):
print(i)
#using set() for removing replicates
basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
for f in sorted(set(basket)):
print(f)
#checking the value of a list with isnan()
import math
raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]
filtered_data = []
for value in raw_data:
if not math.isnan(value):
filtered_data.append(value)
filtered_data