Akash Meghani
Akash Meghani

Reputation: 65

Dictionary encoding in python

Hello guys i am new to python and coding.

I want to group following medications(ICD10 coding) :

A00 - B99    1
C00 - D49    2
D50 - D89    3
E00 - E89    4

I have created the dictionary for mapping which looks like :

dict_ICD_10 = {"A":1,"B":1,"C":2,"D":2,"E":3}

but anyhow this looks incorrect because D50 - D89 are in class 3 but according to my encoding it is coming under class 2. I am scratching my head from morning but not able to figure out.Is there a way to do this coding from a diffrent way.

Thanks in advance

Upvotes: 0

Views: 112

Answers (1)

Dan Breen
Dan Breen

Reputation: 12934

Assuming these are always 3 digits, you can take advantage of alphanumeric sorting, and write something like this:

classes = (
    ('A00', 'B99', 1),
    ('C00', 'D49', 2),
    ('D50', 'D89', 3),
    ('E00', 'E89', 4),
)

def get_class(code):
    for low, high, cls in classes:
        if low <= code <= high:
            return cls
    return None

Note that this will return None if there is no match, and you may also want to enforce a valid 3-character code first (for example get_class('A1') returns 1, but probably should not). Also, get_class('D90') returns None but may not immediately be obvious why (although maybe in that field it is obvious). Depending on the application you may want to do something different if a code falls in between one of these "holes".

Upvotes: 1

Related Questions