Skip to content

Instantly share code, notes, and snippets.

@jeremyjbowers
Created October 29, 2017 13:43
Show Gist options
  • Save jeremyjbowers/5f471da7b144a59d64c3077fe1567c95 to your computer and use it in GitHub Desktop.
Save jeremyjbowers/5f471da7b144a59d64c3077fe1567c95 to your computer and use it in GitHub Desktop.
RACHEL CODEZ
#dictionaries!!
my_dict = {}
my_dict['one_thing'] = "woo"
my_dict['second_thing'] = "wowie"
print(my_dict)
print(my_dict.keys())
print(my_dict.values())
print(my_dict['one_thing'])
my_dict['one_thing'] = "whoops!"
#what happens here?
print(my_dict['one_thing'])
#digression: most common reason I use dictionaries is to count things!
counts = {}
letters_in_my_name = ['r','a','c','h','e','l','e','l','i','s','a','b','e','t','h','s','h','o','r','e','y']
for l in letters_in_my_name:
if l not in counts:
counts[l] = 1
else:
counts[l] += 1
print(counts)
#IMPORTANT NOTE: You can't count on anything to be in any specific order in a dictionary, unlike a list.
#End digression.
import csv
with open('models.csv', 'r') as readfile:
models = list(csv.DictReader(readfile, delimiter='\t'))
print(models)
for model in models:
print(model)
#this thing that gets printed, it doesn't look quite
#the dictionary above, because it's a special kind of
#dictionary called an ordered dictionary that DOES promise
#a specific order, but for our purposes it acts like a dictionary.
#Let's look at the keys!
models[0].keys()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment