-
用三个集合表示三门学科的选课学生姓名(一个学生可以同时选多门课)
a. 求选课学生总共有多少人
b. 求只选了第一个学科的人的数量和对应的名字
c. 求只选了一门学科的学生的数量和对应的名字
d. 求只选了两门学科的学生的数量和对应的名字
e. 求选了三门学生的学生的数量和对应的名字
language = {'小明', '小王', '小白', '小红', '小丽'} mathematics = {'小李', '张三', '小明', '小红', '李四'} english = {'小李', '王五', '小明', '小白', '小智'} # a.求选课学生总共有多少人 student = (language | mathematics | english) print(len(student)) # b.求只选了第一个学科的人的数量和对应的名字 count = language - mathematics - english print(len(count), '人只选择了language', count) # c.求只选了一门学科的学生的数量和对应的名字 a = (language & mathematics) b = (mathematics & english) c = (language & english) new_stu = (a | b | c) stu = (language | mathematics | english) print(len(stu) - len(new_stu), stu - new_stu) # d.求只选了两门学科的学生的数量和对应的名字 a = (language & mathematics) b = (mathematics & english) c = (language & english) new_stu = (a | b | c) stu = (language & mathematics & english) print(len(stu ^ new_stu), (stu ^ new_stu)) # e.求选了三门学生的学生的数量和对应的名字 stu = (language & mathematics & english) print(len(stu), stu)
-
获取列表中出现次数最多的元素
例如: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] nums = [1, 2, 2, 1, 3] new_nums = set(nums) count1 = {} for i in new_nums: count1.setdefault(i, nums.count(i)) values1 = count1.values() for i in count1: if count1[i] == max(values1): print(i)
-
实现给定一个日期,判断这个日期是今年第几天的程序(尝试)
例如:2022/12/31 --> 今年第365天;2022/1/1 --> 今年第1天
year = int(input("年:"))
month = int(input("月:"))
day = int(input("日:"))
days = day
if month > 1:
days += 31
if month > 2:
if (year % 4 == 0 and year % 100 != 0) or year % 400 ==0:
days += 29
else:
days += 28
if month > 3:
days += 31
if month > 4:
days += 30
if month > 5:
days += 31
if month > 6:
days += 30
if month > 7:
days += 31
if month > 8:
days += 31
if month > 9:
days += 30
if month > 10:
days += 31
if month > 11:
days += 30
print(year, '\\', month, '\\', day, "的第", days, "天")