-1

I have a dictionary containing the names and roll numbers of students namely class_details. And I want to print the roll numbers along with the names of students who are absent, the names of the students who are absent are stored in a set .

Source Code :

import pandas as pd
df = pd.DataFrame({'Roll num': [17013001,17013002,17013003,17013004,17013005,17013006,17013007,17013008,17013009,17013010,17013011,17013012,17013013,17013014,17013015,17013016,17013017,17013018,17013019,17013020,17013021,17013022,17013023,17013024,17013025,17013026,17013027,17013028,17013029,17013030,17013201,18013024,18013025,18013026,18013027,18013028,18013029,18013030,18013031,18013032,18013033,18013034,18013035,18013036,18013037,18013038,18013039,18013040,18013041,18013042,18013043,18013044], 
               'Name': ['Anjali Saini', 'Anjali Srivastava', 'Ayushi Solanki', 'Bhawana Saroha', 'Ekta Joshi', 'Harshita Virodhiya', 'Harshita Sehrawat', 'Monika Tehlan', 'Mudita Shiromani', 'Muskan Chahar', 'Neelam Chahal', 'Neeru Bhardwaj', 'Neha Yadav', 'Nishu Rathi', 'Pallavi Singh', 'Pragya Tomar', 'Punerva', 'Saumya Singh', 'Shagun Rana', 'Shaily Bavra', 'Sheena Goyal', 'Shipra Gaur', 'Shivani Pawriya', 'Shiwani Sihan', 'Sonam Bhardwaj', 'Sonika Singh', 'Srishti Rajbhar', 'Sujata', 'Ujjawal Tyagi', 'Vishwa Rana', 'Shalu Yadav', 'Aparna Singh', 'Bharti Kaushik', 'Deepika Yadav', 'Gunika Mehra', 'Heena Saini', 'Himanshi Singh', 'Kajal Rajput', 'Anshu Tomar', 'Krishna Aggarwal', 'Kusum Kundu', 'Monika Kushwaha', 'Monika Shira', 'Neha Kardam', 'Nisha Pahal', 'Osheen Kamboj', 'Preeti Deshwal', 'Priyanka Sharma', 'Renu Malik', 'Renu Bisht', 'Sonia Kadyan', 'Swati Arya']})
class_list = df['Name']
class_set = set(class_list)  
present = pd.DataFrame({'Attendees': ['Anshu Tomar','Aparna Singh','Ayushi Solanki','Bharti Kaushik','Bhawana Saroha','Deepika Yadav','Harshita Sehrawat','Harshita Virodhiya','Heena Saini','Kajal Rajput','Krishna Aggarwal','Kusum Kundu','Monika Shira','Mudita Shiromani','Muskan Chahar','Neha Kardam','Neha Yadav','Nisha Pahal','Nishu Rathi','Pragya Tomar','Preeti Deshwal','Punerva','Renu Bisht','Renu Malik','Saumya Singh','Shaily Bavra','Shalu Yadav','Sheena Goyal','Shivani Pawriya','Sonam Bhardwaj','Sonia Kadyan','Sonika Singh','Sujata','Swati Arya','Ujjawal Tyagi','bala suthar']})
present_list = present['Attendees']
present_set = set(present_list)
pres = len(present_set)
pres_num = pres - 1 #subtracting the extra 1 count for the faculty
abs_num = 52 - pres_num #52 is the strength of the class
 
absent_set = class_set - present_set
print("Attendance of Java Lecture, 27/05")
print("Number of Present Students:", pres_num) 
print("Number of Absentees:", abs_num)
print("Names of Absentees are :", *sorted(absent_set),sep="\n") 
class_details = df.set_index('Name')['Roll num'].to_dict()

My Output:

Attendance of Java Lecture, 27/05
Number of Present Students: 35
Number of Absentees: 17
Names of Absentees are :
Anjali Saini
Anjali Srivastava
Ekta Joshi
Gunika Mehra
Himanshi Singh
Monika Kushwaha
Monika Tehlan
Neelam Chahal
Neeru Bhardwaj
Osheen Kamboj
Pallavi Singh
Priyanka Sharma
Shagun Rana
Shipra Gaur
Shiwani Sihan
Srishti Rajbhar
Vishwa Rana

Desired Output is a list of students containing the roll numbers and names of absent students.

Saumya
  • 1
  • 1
  • Welcome to Stack Overflow. Please review [How to Ask](https://stackoverflow.com/questions/how-to-ask). Questions on this site need to be self-contained, which means no links to code or data on another site. Instead, please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) as a copyable piece of code that can be used for testing as well as your expected output. You can [edit](https://stackoverflow.com/posts/67716231/edit) the question. – AlexK May 27 '21 at 05:51
  • Thanks @AlexK. I have tried my best to do the same now. – Saumya May 27 '21 at 06:18
  • A minimal reproducible example should contain a small **data sample** (either a sample of your actual data or data that you create on your own to match the structure of your actual data) as a **copyable** piece of code. Right now, since we don't know the contents of your CSVs, we don't know anything about the variables you create or how they correspond to your desired output. – AlexK May 27 '21 at 06:33
  • See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391) for more info. – AlexK May 27 '21 at 06:34
  • Thanks @AlexK. I hope it's fine now. – Saumya May 27 '21 at 07:24
  • It appears that you are looking for this: `{k: v for k, v in class_details.items() if k in absent_set}` – AlexK May 27 '21 at 07:40
  • Thanks @AlexK. I used: `print("\n".join("{} : {}".format(v, k) for k, v in class_details.items() if k in absent_set))` to make it more readable :) – Saumya May 28 '21 at 04:52

1 Answers1

0
# Python program to demonstrate
# passing dictionary as argument


# A function that takes dictionary
# as an argument
def func(d):

    for key in d:
        print("key:", key, "Value:", d[key])
     
# Driver's code
D = {'a':1, 'b':2, 'c':3}
func(D)