Reputation: 1693
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
ClosestCommonAncestor("A","C",x)
File "C:\x\x.py", line 149, in ClosestCommonAncestor
b = tree[otu2][0]
KeyError: ('ADBFGC', 14.5)
This is the function that returns the error.
def ClosestCommonAncestor32 (otu1,otu2,tree):
while True:
a = tree[otu1][0][0]
while True:
b = tree[otu2][0]
if a == b:
return a
otu2 = b
otu1 = a
this is the tree input
{'A': [('AD', 4.0), None, None], 'C': [('ADBFGC', 14.5), None, None], 'B': [('BF', 0.5), None, None], 'E': [('ADBFGCE', 17.0), None, None], 'D': [('AD', 4.0), None, None], 'G': [('BFG', 6.25), None, None], 'F': [('BF', 0.5), None, None], 'ADBFG': [('ADBFGC', 6.25), ('AD', 4.25), ('BFG', 2.0)], 'BF': [('BFG', 5.75), ('B', 0.5), ('F', 0.5)], 'ADBFGC': [('ADBFGCE', 2.5), ('ADBFG', 6.25), ('C', 14.5)], 'ADBFGCE': [None, ('ADBFGC', 2.5), ('E', 17.0)], 'BFG': [('ADBFG', 2.0), ('BF', 5.75), ('G', 6.25)], 'AD': [('ADBFG', 4.25), ('A', 4.0), ('D', 4.0)]}
I don't understand this, I thought Keyerror was when it requested a key that didn't exist? Thanks
Upvotes: 1
Views: 4295
Reputation: 91094
Therefore somehow you are setting otu2 = ('ADBFGC', 14.5)
, and probably trying to use that to index into a list which expects an integer.
Upvotes: 1
Reputation: 500317
I thought Keyerror was when it requested a key that didn't exist?
That's right.
However, there is no key ('ADBFGC', 14.5)
in your dictionary. The tuple is indeed present in one of the value lists, but it's not a key. The dictionary's keys are 'A'
, 'C'
, 'ADBFGC'
etc.
To just use the first element of the tuple to index into the dictionary, write tree[otu2[0]]
.
Upvotes: 4