简体   繁体   English

如何通过将集合作为参数传递来打印字典的键和值?

[英]How can I print the keys and values of a dictionary by passing a set as an argument?

I have a dictionary containing the names and roll numbers of students namely class_details.我有一本包含学生姓名和卷号的字典,即 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:我的 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. Desired Output 是包含缺勤学生的卷号和姓名的学生列表。

# 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何添加所有键的值并打印新词典? - How can I add all keys' values and print the new dictionary? 给定键适合特定间隔,如何在python中打印出字典的值? - How can I print out the values of a dictionary in python given the keys fit a certain interval? 如何仅打印 5 次 Python 字典的键和值? - How can I print out the keys and values of a Python dictionary only 5 times? Python:具有多个值的键的字典。 如何将字典打印为分隔值列表的字典集 - Python: dictionary with keys having multiple values. How to print dictionary as set of dictionaries separating the list of values 如何从另一个python字典中的一个字典中获得相应的列表值,在列表中它们被列为键,比较并打印出csv? - How can I get corresponding list values in one dictionary from another python dictionary where they are listed as keys, compare and print out a csv? 我如何记录函数参数的字典键? - How can i document dictionary keys for a functions argument? 如何在没有按键的情况下打印字典的元素? - how can i print the elements of a dictionary without the keys? 如何在python中打印字典键和值 - How to print dictionary keys and values in python 如何为特定键返回字典值的常用值? - How can I return common values of dictionary values for specific keys? 如何获取字典键组合的唯一值集? - How do I get the unique set of values for a combination of dictionary keys?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM