Skip to content

Instantly share code, notes, and snippets.

@wray
Last active March 25, 2017 18:25
Show Gist options
  • Save wray/838cef30c329edef712d68f6f349ab21 to your computer and use it in GitHub Desktop.
Save wray/838cef30c329edef712d68f6f349ab21 to your computer and use it in GitHub Desktop.
# Pre-req's : loops, conditionals, functions, lists, tuples, and dictionaries
#
# Python 2.7
#
# Objectives : Advanced dictionaries, handling key collisions
# Remember in the previous lesson, we defined a contact record using the following tuple
contact = ('first_name','last_name','mobile_phone','home_phone','zip_code')
FIRST_NAME = 0 # First name is in the first position of the tuple.
LAST_NAME = 1 # Last name i sin the second position of the tuple...
MOBILE_PHONE = 2
HOME_PHONE = 3
ZIP_CODE = 4
# We built a simple function to help us create contact tuples
def create_contact(first_name,last_name,mobile_phone,home_phone=None,zip_code=None):
return (first_name,last_name,mobile_phone,home_phone,zip_code)
# And we had some concerns about how to handle contacts with the same first or last name.
# So, we'll add some contacts with name overlaps:
contacts = []
contacts.append(create_contact("Sue","Simmons","804-555-1234"))
contacts.append(create_contact("John","Doe","804-555-1235","804-555-1236"))
contacts.append(create_contact("Pat","Petri","804-555-1237","804-555-1238","23117"))
contacts.append(create_contact("John","Smith","804-555-4321","804-555-4322"))
contacts.append(create_contact("Steve","Simmons","804-555-4323","804-555-4323","23117"))
# We know we want to create separate dictionaries to help us find by first name or last name.
# We'll want to create a function that keeps our dictionaries in sync. Thus, we really only need
# to think about how to deal with what to return when one searches for first name using "John".
# Remember, a Python dictionary doesn't reall "care" what you are putting in the dictionary.
# If we know there could be more than 1 contacts with the same first name, why not store a list
# of contacts by first name? Right?? Make sense? Let's look at the code:
contacts_by_first_name = {}
# The "trick" is that this dictionary will store a list of contacts as its value.
# In other words, the structure will be like this: { first_name2: [contact1, contact2, ...], ... }
for contact in contacts:
# We want to see if there is already an entry for the key
first_name = contact[FIRST_NAME]
if first_name in contacts_by_first_name.keys():
contacts_by_first_name[first_name].append(contact)
else:
contacts_by_first_name[first_name] = [contact]
# Follow that?
# Essentially, if there is already a contact in the dictionary, append the other one with the
# same key to the list of contacts with that same key.
# If there isn't a contact there, create a list with just the one contact.
# let's look at what the dictionary looks like:
print
print "A dictionary (index) that handles key collisions, by storing the list of matching contacts in the value."
print contacts_by_first_name
# So, now, the find by first_name will return a list of results, which is exactly what one would expectx!
contact = contacts_by_first_name["John"]
print
print "Find by first_name 'John'"
print contact
# This is pretty neat, right? Python doesn't care what the value is... a tuple, a string, or, in
# this case, a list of tuples! Wait 'til we discuss dictionaries of dictionaries (of dictionaries)...
# Can you write the code for contacts_by_last_name?
contacts_by_last_name = {}
# Let's look at the code for the add_contact method now:
def add_contact(contact):
first_name = contact[FIRST_NAME]
if first_name in contacts_by_first_name.keys():
contacts_by_first_name[first_name].append(contact)
else:
contacts_by_first_name[first_name] = [contact]
last_name = contact[LAST_NAME]
if last_name in contacts_by_last_name.keys():
contacts_by_last_name[last_name].append(contact)
else:
contacts_by_last_name[last_name] = [contact]
add_contact(create_contact("Bart","Simpson","804-555-4321"))
print
print "What do your dictionaries look like now?"
print contacts_by_first_name
print contacts_by_last_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment