-
用三个集合表示三门学科的选课学生姓名(一个学生可以同时选多门课)
a. 求选课学生总共有多少人
b. 求只选了第一个学科的人的数量和对应的名字
c. 求只选了一门学科的学生的数量和对应的名字
d. 求只选了两门学科的学生的数量和对应的名字
e. 求选了三门学生的学生的数量和对应的名字
CurriculaName_Chinese = {'张三', '李四', '王五', '麻子'} CurriculaName_Math = {'张三', '王五', '旺财', '包子', '狗子', '李四'} CurriculaName_English = {'财财', '李四', '狗子'} # a. new_CurriculaName = set() new_CurriculaName = CurriculaName_Chinese | CurriculaName_Math | CurriculaName_English print(new_CurriculaName) print('选课学生总人数:', len(new_CurriculaName)) # b. count = 0 name = [] for x in CurriculaName_Chinese: if (x not in CurriculaName_Math) and (x not in CurriculaName_English): count += 1 name.append(x) print(count, name) # c. name = CurriculaName_Chinese ^ CurriculaName_Math ^ CurriculaName_English count = 0 for x in name: count += 1 print('只选了一门学科的学生:', name, count) # d. print('选了语文和数学两门学科的学生:', CurriculaName_Chinese & CurriculaName_Math) print('选了语文和英语两门学科的学生:', CurriculaName_Chinese & CurriculaName_English) print('选了数学和英语两门学科的学生:', CurriculaName_English & CurriculaName_Math) # e. print('选了三门课的学生:', CurriculaName_Chinese & CurriculaName_Math & CurriculaName_English)
-
获取列表中出现次数最多的元素
例如: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) # print(new_nums) count1 = {} for x in new_nums: count1.setdefault(x, nums.count(x)) print(count1) all_values = count1.values() # print(max(all_values)) for x in count1: if count1[x] == max(all_values): print('列表中出现次数最多的元素:', x)
-
实现给定一个日期,判断这个日期是今年第几天的程序(尝试)
例如:2022/12/31 --> 今年第365天;2022/1/1 --> 今年第1天
year = int(input('请输入年:')) mouth = int(input('请输入月:')) day = int(input('请输入天:')) CumulativeDaysYear = [(1, 31), (2, 59), (3, 90), (4, 120), (5, 151), (6, 181), (7, 212), (8, 243), (9, 273), (10, 304), (11, 334), (12, 365)] # print(31+28+31+30+31+30+31+31+30+31+30+31) CumulativeDays_LeapYear = [(1, 31), (2, 60), (3, 91), (4, 121), (5, 152), (6, 182), (7, 213), (8, 244), (9, 274), (10, 305), (11, 335), (12, 366)] if mouth == 1: print('今年第', day, '天') else: if year % 400 == 0 or (year % 100 != 0 and year % 4 == 0): for x in range(len(CumulativeDays_LeapYear)): if mouth == CumulativeDays_LeapYear[x][0]: print('今年第', CumulativeDays_LeapYear[x - 1][1] + day, '天') else: for x in range(len(CumulativeDaysYear)): if mouth == CumulativeDaysYear[x][0]: print('今年第', CumulativeDaysYear[x - 1][-1] + day, '天')
day9集合作业
最新推荐文章于 2024-09-09 18:34:56 发布