Python training exercise 6
Introduction
So far we've seen variables that store one value or a series of values. There is another way of storing information where you associate one variable with another; in Python this is called a dictionary. Dictionaries provide a very useful way of quickly connecting variables to each other.
Exercises
Dictionary creation and usage
Dictionaries are initiated by using curly brackets { }, you can then recall or add values by using square brackets [ ]:
myDictionary = {'A': 'Ala', 'C': 'Cys', 'D': 'Asp'} oneLetterCode = 'A' print(oneLetterCode, myDictionary[oneLetterCode]) myDictionary['E'] = 'Glu' print(myDictionary)
The reference value is called a dictionary key, which refers to a value. Dictionaries, like lists, have several useful built-in methods:
myDictionary = {'A': 'Ala', 'C': 'Cys', 'D': 'Asp', 'E': 'Glu'} print(list(myDictionary.keys())) print(list(myDictionary.values()))
If you try to access a key that doesn't exist, Python will give an error:
myDictionary = {'A': 'Ala', 'C': 'Cys', 'D': 'Asp', 'E': 'Glu'} print(myDictionary['B'])
You should therefore always check whether a key exists:
# Newlines don't matter when initialising a dictionary... myDictionary = { 'A': 'Ala', 'C': 'Cys', 'D': 'Asp', 'E': 'Glu', 'F': 'Phe', 'G': 'Gly', 'H': 'His', 'I': 'Ile', 'K': 'Lys', 'L': 'Leu', 'M': 'Met', 'N': 'Asn', 'P': 'Pro', 'Q': 'Gln', 'R': 'Arg', 'S': 'Ser', 'T': 'Thr', 'V': 'Val', 'W': 'Trp', 'Y': 'Tyr'} # Note that line below can also be written as: "if myDictionary.has_key('B'):". # This syntax is obsolete in Python 3.x however. if 'B' in myDictionary.keys(): print(myDictionary['B']) else: print("myDictionary doesn't have key 'B'!")
Use a dictionary to track how many times each amino acid code appears in the following sequence:
SFTMHGTPVVNQVKVLTESNRISHHKILAIVGTAESNSEHPLGTAITKYCKQELDTETLGTCIDFQVVPGCGISCKVTNIEGLLHKNNWNIEDNNIKNASLVQIDASNEQSSTSSSMIIDAQISNALNAQQYKVLIGNREWMIRNGLVINNDVNDFMTEHERKGRTAVLVAVDDELCGLIAIADT Tip: use the one-letter code as key in the dictionary, and the count as value. [click on show more for answer) |
---|
mySequence = "SFTMHGTPVVNQVKVLTESNRISHHKILAIVGTAESNSEHPLGTAITKYCKQELDTETLGTCIDFQVVPGCGISCKVTNIEGLLHKNNWNIEDNNIKNASLVQIDASNEQSSTSSSMIIDAQISNALNAQQYKVLIGNREWMIRNGLVINNDVNDFMTEHERKGRTAVLVAVDDELCGLIAIADT" # First way to do this (condensed) aminoAcidCount = {} myUniqueAminoAcids = set(mySequence) for aaCode in myUniqueAminoAcids: print("Amino acid {} occurs {} times.".format(aaCode,mySequence.count(aaCode))) aminoAcidCount[aaCode] = mySequence.count(aaCode) # Second way to do this (more elaborate) aminoAcidCount = {} for aaCode in mySequence: if not aaCode in aminoAcidCount.keys(): aminoAcidCount[aaCode] = 0 # Or aminoAcidCount[aaCode] += 1 aminoAcidCount[aaCode] = aminoAcidCount[aaCode] + 1 aaCodes = aminoAcidCount.keys() aaCodes.sort() for aaCode in aaCodes: print("Amino acid {} occurs {} times.".format(aaCode,aminoAcidCount[aaCode])) |
More with dictionaries
You can also make a dictionary refer to lists, or other dictionaries:
# Create a dictionary with a list of names and a number-based dictionary # with an identification number referring to information about a person mainDict = {} mainDict['myNames'] = ['Jack','Joe','Anne','Julia','Dennis','Yuri','Mel'] mainDict['myIds'] = {5343: ('Male', 'Jack', 34), 3432: ('Female','Anne', 25), 7345: ('Male', 'Yuri', 53)} # Loop over the values in myList in the dictionary for name in mainDict['myNames']: # Check whether we find this name in the myIds dictionary nameFound = False # Loop over all the information for the id numbers, # and check whether the name matches for idNumber in mainDict['myIds'].keys(): # Get the information out - take care here to not use # the variable 'name' again or we will overwrite the original value! (gender,nameInDict,age) = mainDict['myIds'][idNumber] # Check whether the names match if name == nameInDict: print ("{} has ID number {}".format(name,idNumber)) nameFound = True break # If no match was found, print out that this person has no ID number if not nameFound: print ("No ID number found for {}".format(name))
You can, however, only use variables that cannot change a keys (so tuples are OK, lists are not), and keys have to be unique: if you add a key that already exists, the old entry will be overwritten:
mySample = {'pH': 5.6, 'temperature': 288.0, 'volume': 200, 'name': 'calibration_1'} if __name__ == '__main__': print(mySample['pH']) mySample['pH'] = 7.0 print(mySample['pH']) # This is fine moleculeKey = ('protein','mySmallPeptide') mySample[moleculeKey] = 'ASKLPIITREWSDDN' # This will fail... otherMoleculeKey = ['DNA','myDna'] mySample[otherMoleculeKey] = 'TGCATTGCCA'
You will get the error TypeError: unhashable type: 'list'.