【发布时间】:2021-12-30 17:35:22
【问题描述】:
我正在编写一个函数,它接受一个“主要”并通过字典中的值对其进行迭代。正确的输出是如果专业在值中,则打印键(1 个实例),如果不只是将“专业”打印为上层()。但我不知道如何编写它来检查每个值,但如果值在其中,则只打印键的一个实例,而不是其他 16 个值的多种大写格式。
输入: 教育, 计算机科学
输出: 教育, 比较
student_majors_vari = {
'COMP':['compsci','comp sci','computer sci','computer science'],
'CMPE':['compeng', 'comp eng', 'computer eng', 'computer engineering'],
'EE':['eleceng', 'elec eng', 'elec engineering', 'electrical engineering'],
'MATH':['mathsci', 'math sci', 'mathematical sci', 'mathmatical sciences', 'mathematics']
}
def get_major_code(major):
for univ_majors in student_majors_vari:
values = student_majors_vari[univ_majors]
for v in values:
if major == v or major == values:
print(univ_majors)
else:
print(major.upper())
【问题讨论】:
-
请提供示例输出
标签: python list loops dictionary nested