-
用三个集合表示三门学科的选课学生姓名(一个学生可以同时选多门课)
math = {'坤坤', '鲲鲲', '鸽鸽', '小黑子', '只因'} english = {'坤坤', '真正的man', '小黑子', '鸡爷'} science = {'鲲鲲', '真正的man', '干嘛', '哎哟', '小黑子'}
a. 求选课学生总共有多少人
score = math | english | science print(len(score))
b. 求只选了第一个学科的人的数量和对应的名字
score = math - english - science print(len(score), score)
c. 求只选了一门学科的学生的数量和对应的名字
count = 0 score = list(math) + list(english) + list(science) list1 = [] for x in score: if score.count(x) == 1: list1.append(x) count += 1 print(count, list1)
d. 求只选了两门学科的学生的数量和对应的名字
count = 0 score = list(math) + list(english) + list(science) list1 = [] for x in score: if score.count(x) == 2: list1.append(x) count += 1 print(int(count/2), set(list1))
e. 求选了三门学生的学生的数量和对应的名字
score = math & english & science print(len(score), score)
-
获取列表中出现次数最多的元素
例如: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] s1 = set(nums) max = 0 content = [] for i in s1: if max < nums.count(i): max = nums.count(i) content.clear() content.append(i) elif max == nums.count(i): content.append(i) print('出现次数最多的元素是:',content)
-
实现给定一个日期,判断这个日期是今年第几天的程序(尝试)
例如:2022/12/31 --> 今年第365天;2022/1/1 --> 今年第1天
day9-集合作业
最新推荐文章于 2024-09-09 18:34:56 发布