Reputation: 45
Say someone was making a grade system, and they had a dictionary with each grade letter as value and a percentage as a key. How would you be able to pick a value if say, you had your key as 75% and you wanted it to print "C", but then it would still print "C" if you entered 74% or 79%.
Upvotes: 0
Views: 241
Reputation: 30481
I'd suggest using a list of tuples containing grade, score
-pairs as this would allow you the freely specify how percentage scores are mapped into grades:
grades = [
('A', 90),
('B', 80),
('C', 70),
('D', 60),
('F', None)
]
def get_grade(score):
return [grade for grade, grade_score in grades if score>=grade_score][0]
Usage:
print get_grade(45)
Upvotes: 1
Reputation: 309821
I would use something like:
d = {10:'A', 9:'A', 8:'B', 7:'C', 6:'D', 5:'F', 4:'F', 3:'F', 2:'F', 1:'F', 0:'F'}
print d[int(95.0/10)] #same as `d[95//10]`.
If you don't like all that typing, you could initialize your dict as:
d = dict(zip(range(10,-1,-1),'AABCDFFFFFF'))
The problem with your proposal is that you'll probably end up using floating point numbers as keys, and due to shortcomings of floating point operations, it's extremely hard to use them as dictionary keys. This maps floats to integers and then uses those integers as keys in the dictionary which is a lot cleaner.
You could even be a little more clever if you wanted:
class GradeBook(dict):
def __getitem__(self,key):
return dict.__getitem__(self,key//10)
def __missing__(self,key):
if key > 9:
return 'A'
elif key < 5:
return 'F'
d = GradeBook(zip(range(5,10),'FDCBA'))
print d[100]
print d[45]
print d[120]
print d[45.2]
print d[72.8]
Upvotes: 4
Reputation: 6851
I would use numpy and define bins. Binning of data is a common problem.
import numpy as NP
grades = NP.random.randint(0, 100, 30)
bins = NP.array([0., 20., 40., 60., 80., 100.])
# d is an index array holding the bin id for each point in A
d = NP.digitize(grades, bins)
cat = ['A','B','C','D','E']
g = [cat[i-1] for i in d]
print grades,'\n'
print bins,'\n'
print g,'\n'
'''
output
[80 75 65 2 41 79 49 64 68 18 88 0 60 90 79 96 42 0 43 8 7 36 96 22 96
18 17 80 54 34]
[ 0. 20. 40. 60. 80. 100.]
['E', 'D', 'D', 'A', 'C', 'D', 'C', 'D', 'D', 'A', 'E', 'A', 'D', 'E', 'D', 'E'\
, 'C', 'A', 'C', 'A', 'A', 'B', 'E', 'B', 'E', 'A', 'A', 'E', 'C', 'B']
'''
Upvotes: 1
Reputation: 2885
grades = {60: 'F', 70: 'D', 80: 'C', 90: 'B', 100: 'A'}
def get_grade(percent):
for k in sorted(grades):
if k >= percent:
return grades[k]
if __name__ == '__main__':
print get_grade(50)
print get_grade(60)
print get_grade(71)
print get_grade(97)
In this case, the grade will that corresponding to the lowest key which is greater than or equal to the percentage.
Upvotes: 0
Reputation: 1791
Doctor Livingston, I Presume.
No but really, this is not how a dictionary works. A dictionary maps a value to a key. What you're looking for is a function that transforms the value to a letter.
def grade_value(grade):
if grade == 100:
return "A is for Awesome!"
if grade > 50:
return "F is for FAILURE!!!"
Okay, so this isn't an actual example. But, I hope you can adapt this so it works for you.
Upvotes: 4