-
用三个集合表示三门学科的选课学生姓名(一个学生可以同时选多门课)
a. 求选课学生总共有多少人
b. 求只选了第一个学科的人的数量和对应的名字
c. 求只选了一门学科的学生的数量和对应的名字
d. 求只选了两门学科的学生的数量和对应的名字
e. 求选了三门学生的学生的数量和对应的名字
chi = {'张三','李四','王五','老六','小七','潘八'} mat = {'张三','王五','小七','潘八','李四'} eng = {'潘八','小七','王五','小明','小红'} # a print(len(chi|mat|eng)) # b print(len(chi-mat-eng),chi-mat-eng) # c print(chi - mat - eng) print(mat - chi - eng) print(eng - chi - mat) # d for i in chi&mat: if i not in eng: print(i) for j in chi&eng: if j not in mat: print(j) for x in mat&eng: if x not in chi: print(x) # e # 法1 a = set() for i in chi: for j in mat: for x in eng: if x == j == i: a.add(x) print(len(a),a) # 法2 print(len(chi & mat & eng),chi & mat & eng)
-
获取列表中出现次数最多的元素
例如:nums = [1, 2, 3,1,4,2,1,3,7,3,3] —> 打印:3
nums = [1,2,2,1,3] --> 打印1、2
new_nums = {} nums = [1, 2, 3,1,4,2,1,3,7,3,3] for i in nums: if nums.count(i) > 1: new_nums[i] = nums.count(i) for i in new_nums: if new_nums[i] == new_nums[max(new_nums)]: print(f'出现次数最多元素为{i}')
-
实现给定一个日期,判断这个日期是今年第几天的程序(尝试)
例如:2022/12/31 --> 今年第365天;2022/1/1 --> 今年第1天
dates = '2020/12/31'
dates1 = dates.split("/")
month = int(dates1[1])
year = int(dates1[0])
day = int(dates1[2])
month_days = [0,31,28,31,30,31,30,31,31,30,31,30,31]
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
month_days[2] = 29
count_ = 0
for i in range(month):
count_ += month_days[i]
count_ += day
print(count_)