I have a list of items that I need to bubble sort. The bubble sort criteria is if item j is ranked higher than item i in a majority of result sets, swap item j with item i in FullList. I have a very simple bubble sort figured out except for one small problem. I get a key error when the FullList item doesn’t appear in one of the result sets. I need to put in a value to compensate for that or else my loop becomes very complicated with numerous if statements. If I could assign a token value such as 20 to any value that doesn’t exist in the dictionary, my loop will be perfect. Can anyone help me out?
FullList = [B,C,A,D,H,E,F,G]
Results1 = {'A':1,'B':2,'C':3,'D':4,'E':5}
Results2 = {'B':1,'D':2,'G':3,'F':4,'E':5}
Results3 = {'C':1,'D':2,'B':3,'A':4,'H':5}
Pseudo Code:
switch = True
while(switch):
switch = False
for i in range(len(FullList)-1):
if FullList[i+1]<FullList[i] in Results1 & 2:
FullList[i],FullList[i+1] = FullList[i+1],FullList[i]
switch = True
elif FullList[i+1]<FullList[i] in Results1 & 3:
FullList[i],FullList[i+1] = FullList[i+1],FullList[i]
switch = True
elif FullList[i+1]<FullList[i] in Results2 & 3:
FullList[i],FullList[i+1] = FullList[i+1],FullList[i]
switch = True
Key-Error: 'A' not in 'Results2'
don’t know exactly what you are trying to do, but seems like
get(key[, default])is what you need, you can check out the details of this hereIn your case,
Results2.get('A', 20)will give you 20