mardi 2 novembre 2021

I am confused about how to add values in the Other section

Write a python program that will make a dictionary from the given list of Student IDs based on the department and admitted year.
ID explanation:
First 2 digits: Year
4th and 5th digit: Department 01 = CSE, 41=CS, 21=EEE, Any other digits = Other
For example, ID: 21121347, in this ID, the first 2 digits are 21. So the year is 2021.
The 4th and 5th digit are 21. So the department in EEE.
Given List [Your code should work for all types of similar IDs if the given List is changed.]:
IDs = ['20201199','21121347','20141052','20341121','21241369','21272199','19241187','19101007','20101035', '21121875', '19141534', '19301552', '21141135', '21365001']

Sample Output (You have to print the resultant dictionary only)

{
     2020:{
              'CSE': ['20201199', '20101035'],
              'CS': ['20141052', '20341121']
              },
     2021:{
              'EEE': ['21121347', '21121875'],
              'CS': ['21241369', '21141135'],
              'Other': ['21272199', '21365001']
              },
     2019:{
              'CS': ['19241187', '19141534'],
              'CSE': ['19101007', '19301552']
             }
}

My Code:

IDs = ['20201199','21121347','19101052','19301121','20221369','21241199','19241187','20141007','20101035', '21121875', '19141534', '20341552', '20121135', '21341001']

dct = {}
dptname = {'01':'CSE','41':'CS', '21':'EEE', "Other": }
for idx in IDs:
    yr = '20' + idx[:2]
    dpt = dptname[idx[3:5]]
    if yr in dct:
        if dpt in dct [yr]:
            dct[yr][dpt].append (idx)
        else:
            dct[yr][dpt] = [idx]
    else:
        dct[yr] = {dpt : [idx]}

print (dct)
print ()

I am mainly confused how to add to the Other in dictonary

Aucun commentaire:

Enregistrer un commentaire