用三个集合表示三门学科的选课学生姓名(一个学生可以同时选多门课)
math_class = {"tian", "wang", "zhou", "sun"}
eng_class = {"tian", "li", "chen", "hu"}
cha_calss = {"tian", "zhao", "chen", "zhu"}
a. 求选课学生总共有多少人
student_len = math_class | eng_class | cha_calss
print(len(student_len)) # 9
b. 求只选了第一个学科的人的数量和对应的名字
first_student = math_class - (math_class & eng_class) - (math_class & cha_calss)
print(len(first_student), first_student) # 3 {'wang', 'zhou', 'sun'}
c. 求只选了一门学科的学生的数量和对应的名字
only_math = math_class - (math_class & eng_class) - (math_class & cha_calss)
only_eng = eng_class - (eng_class & math_class) - (eng_class & cha_calss)
only_cha = cha_calss - (cha_calss & math_class) -(cha_calss & eng_class)
print("only_math_len = {}, name = {}".format(len(only_math), only_math))
print("only_eng_len = {}, name = {}".format(len(only_eng), only_eng))
print("only_cha_len = {}, name = {}".format(len(only_cha), only_cha))
"""
only_math_len = 3, name = {'zhou', 'sun', 'wang'}
only_eng_len = 2, name = {'hu', 'li'}
only_cha_len = 2, name = {'zhao', 'zhu'}
"""
d. 求只选了两门学科的学生的数量和对应的名字
math_and_eng = (math_class & eng_class) - (math_class & eng_class & cha_calss)
math_and_cha = (math_class & cha_calss) - (math_class & eng_class & cha_calss)
eng_and_cha = (eng_class & cha_calss) - (math_class & eng_class & cha_calss)
print("math_and_eng = {}, name = {}".format(len(math_and_eng), math_and_eng))
print("math_and_cha = {}, name = {}".format(len(math_and_cha), math_and_cha))
print("eng_and_cha = {}, name = {}".format(len(eng_and_cha), eng_and_cha))
"""
math_and_eng = 0, name = set()
math_and_cha = 0, name = set()
eng_and_cha = 1, name = {'chen'}
"""
e. 求选了三门学生的学生的数量和对应的名字
math_eng_cha = math_class & eng_class &cha_calss
print("math_eng_cha = {}, name = {}".format(len(math_eng_cha),math_eng_cha))
"""
math_eng_cha = 1, name = {'tian'}
"""
获取列表中出现次数最多的元素
例如:nums = [1, 2, 3,1,4,2,1,3,7,3,3] —> 打印:3
nums = [1,2,2,1,3] --> 打印1、2
nums = [1, 2, 3, 1, 4, 2, 1, 3, 7, 3, 3, 1]
set_nums = set(nums)
count_dict = {}
count_list = []
for item in set_nums:
count_list.append(nums.count(item))
count_dict.setdefault(item, nums.count(item))
max_count =max(count_list)
for key,value in count_dict.items():
if value == max_count:
print(key)
实现给定一个日期,判断这个日期是今年第几天的程序(尝试)
例如:2022/12/31 --> 今年第365天;2022/1/1 --> 今年第1天
import time
print(time.localtime().tm_yday)