-
用三个集合表示三门学科的选课学生姓名(一个学生可以同时选多门课)
a. 求选课学生总共有多少人
math = {'好好','学学','嘻嘻','alei','tian','007'} physics = {'好1','学1','嘻1','al11','ti11','01','alei','tian','007'} chinese = {'好2','01','alei','ti2','005'} print(len(math|physics|chinese))
b. 求只选了第一个学科的人的数量和对应的名字
print(math-physics-chinese) # {'嘻嘻', '好好', '学学'} print(len(math-physics-chinese)) #3
c. 求只选了一门学科的学生的数量和对应的名字
one = (math - physics -chinese)|(physics - math -chinese)|(chinese-math-physics ) print(one,len(one)) #{'ti11', '学学', '好1', '学1', '好2', 'al11', '005', 'ti2', '嘻1', '好好', '嘻嘻'} 11
d. 求只选了两门学科的学生的数量和对应的名字
two = ((math&chinese)|(physics&math)|(physics&chinese))-math&physics&chinese print(two,len(two)) #{'01'} 1
e. 求选了三门学生的学生的数量和对应的名字
three = math&chinese&physics print(three,len(three)) #{'alei'} 1
-
获取列表中出现次数最多的元素
例如: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] nums1 = set(nums) max1 = nums.count(1) list = [1] for x in nums1: if nums.count(x) > max1: max1 = nums.count(x) list.clear() list.append(x) elif nums.count(x) == max1 : list.append(x) print(set(list)) #方法2 nums = [1, 2, 3,1,4,2,1,3,7,3,3] max1 = nums.count(1) nums1 = set(nums) list = [] for x in nums1: if nums.count(x) > max1: max1 = nums.count(x) list.clear() list.append(x) for x in nums1: if nums.count(x) == max1: list.append(x) print(set(list))
-
实现给定一个日期,判断这个日期是今年第几天的程序(尝试)
例如:2022/12/31 --> 今年第365天;2022/1/1 --> 今年第1天
day9集合作业
最新推荐文章于 2023-03-17 09:01:50 发布