1414#Classes have a function __hash__ invoked when used as the key
1515print (hash ("hello" ))
1616
17- #Almost always immutable type
17+ #I'm not sure of exact internals on how the hash is used, but imagine it like so:
18+ #You have an area of memory with 8 spots, and you need to store the value at some spot...
19+ print (hash ("hello" ) % 8 )
20+
21+ #Almost always immutable type (should be, anyway)
1822#a tuple will work, list will not. a number will work
1923
2024#Why use a hashtable? Extremely fast to add or look up data
2630
2731
2832######### RETRIEVE DATA FROM DICTIONARY ##########
33+
34+
2935print (list (emails ))
3036print (sorted (emails ))
3137#print(emails[0]) # NOPE!
7379
7480######### LOOPING THROUGH KEYS #########
7581
82+
7683#dictionary is an iterable (implements __iter__)
7784
7885emails = {
8693 print (k )
8794
8895#You can use the key to get the element
96+ #Not ideal.
97+ #One reason being the key has to be hashed to get the value associated with it.
8998#(but will show better way in next section)
9099for k in emails :
91100 print ("index" , k , "is" , emails [k ])
97106#In the prev section we used the index with [].
98107#Although it works, you can do this:
99108
100- for k , elem in emails :
109+ for k , elem in emails . items () :
101110 print (k , elem )
102111
103112#Each iteration k will be the key and elem will be the item found at this key.
104113
114+ #As an example of what a hashtable can be used for, you can keep track of occurances:
115+
116+ conjunctions = {"but" : 0 , "or" : 0 , "so" : 0 , "and" : 0 , "yet" : 0 , "for" : 0 , "nor" : 0 } #fanboys
117+
118+ completely_original_poem = """I still hear your voice when you sleep next to me
119+ I still feel your touch in my dreams
120+ Forgive me my weakness, but I don't know why
121+ Without you it's hard to survive
122+ 'Cause every time we touch, I get this feeling
123+ And every time we kiss I swear I could fly
124+ Can't you feel my heart beat fast, I want this to last
125+ Need you by my side"""
126+
127+ words = completely_original_poem .split ()
128+
129+ for word in words :
130+ if str .lower (word ) in conjunctions :
131+ conjunctions [str .lower (word )] += 1
132+
133+ print (conjunctions )
134+
135+ #This could easily be wrapped in a function to take a msg and words to look for, returning a dict
136+ #concept can be used to analyze documents to quantify how vulgar they are, search for phrases, etc
137+ #dictionaries can be used to keep track of values that are hard to calculate (memoization)
138+
105139
106140######### SETS EXPLAINED #########
107141
110144#Sets are similar to lists in that they just contain the data and not a key-value pair
111145#Sets are different than lists in that you cannot have duplicates
112146
147+ stuff = {"sword" , "rubber duck" , "sice a pizza" }
148+ print ("sword" in stuff )
149+ print (stuff )
150+ stuff .add ("sword" )
151+ print (stuff )
152+ #Notice only one occurance of sword even though already added
153+
154+ #How is a set different than a dictionary?
155+ #For a set, each element is only one piece of data
156+ #for a dictionary, it is a key-value pair.
157+
158+ #Behind the scenes, they both use hashing. The hashing is used to determine where to store the data.
159+ #For dictionaries, the KEY is hashed
160+ #for sets, we do not have a key, so the data itself is hashed.
161+ #This means we cannot store something in sets that is not hashable.
162+
163+ #stuff.add(["trying to add a list"])
164+
165+ #It's important to understand the purpose of a set...
166+ #Easily check if element in set
167+ #such as to easily check to see if something has been tagged
168+ #To do various set operations (coming soon)
169+
170+ #An example would be to see if a word is ever used in a phrase. Not counted (that wold be a dictionary)
171+
172+ conjunctions = {"but" , "or" , "so" , "and" , "yet" , "for" , "nor" } #fanboys
173+ seen = set () #THERE'S NOT AN EMPTY SET LITERAL!! #learn something new every day
174+ completely_original_poem = """I still hear your voice when you sleep next to me
175+ I still feel your touch in my dreams
176+ Forgive me my weakness, but I don't know why
177+ Without you it's hard to survive
178+ 'Cause every time we touch, I get this feeling
179+ And every time we kiss I swear I could fly
180+ Can't you feel my heart beat fast, I want this to last
181+ Need you by my side"""
182+
183+ words = completely_original_poem .split ()
184+
185+ for word in words :
186+ if str .lower (word ) in conjunctions :
187+ seen .add (str .lower (word ))
188+
189+ print (seen )
113190
114- #we used set with list com STOPPED HERE
115191
116192######### REMOVE DUPLICATES FROM LIST / CREATE SET FROM LIST ##########
193+
194+ #You can remove duplicate elements from a list by converting it to a set and back.
195+
196+ colors = ["red" , "red" , "green" , "green" , "blue" , "blue" , "blue" ]
197+
198+ print (id (colors ), colors )
199+
200+ colors [:] = list (set (colors ))
201+
202+ print (id (colors ), colors )
203+
204+
205+ #Earlier on in our life I showed some code to count each type of element in a list.
206+
207+ colors = ["red" , "red" , "green" , "green" , "blue" , "blue" , "blue" ]
208+
209+ counts = [[colors .count (item ), item ] for item in set (colors )]
210+
211+ print (counts )
212+
213+ #This works because is iterates through the set {"red", "green", "blue"} counting each in colors
214+
215+
117216######### UNION AND INTERSECTION #########
217+
218+ my_fav = {"red" , "green" , "black" , "blue" , "purple" }
219+ her_fav = {"blue" , "orange" , "purple" , "green" }
220+
221+ #union
222+ all_favs = my_fav | her_fav
223+ print (all_favs ) #no repetition
224+ #You may see + to combine lists, in which there are repeats.
225+ #But we are not working with lists...so i'll try to focus here.
226+
227+ #intersection (elements shared between both)
228+ wedding_colors = my_fav & her_fav
229+ print (wedding_colors )
230+ #this is like the inside section of a venn diagram
231+
232+ #There are also method versions:
233+ all_favs = my_fav .union (her_fav )
234+ print (all_favs )
235+
236+ wedding_colors = my_fav .intersection (her_fav )
237+ print (wedding_colors )
238+
239+
118240######### DIFFERENCE AND SYMMETRIC DIFFERENCE #########
241+
242+
243+ my_fav = {"red" , "green" , "black" , "blue" , "purple" }
244+ her_fav = {"blue" , "orange" , "purple" , "green" }
245+
246+ #Difference
247+ only_my_colors = my_fav - her_fav
248+ print (only_my_colors ) #elements in left getting rid of all in right.
249+ #Could go other way too:
250+ only_her_colors = her_fav - my_fav
251+ print (only_her_colors )
252+
253+ #symmetric difference is like if you took colors only I liked union with colors only she liked and put em together:
254+
255+ symmetric = my_fav ^ her_fav
256+ print (symmetric )
257+
258+ #This is like:
259+ symmetric = only_my_colors | only_her_colors
260+ print (symmetric )
0 commit comments