-
用三个集合表示三门学科的选课学生姓名(一个学生可以同时选多门课)
set1 = {'王大炮','李放炮','宋大炮','迪丽热巴','古力娜扎','吴进炮',} set2 = {'李放炮','宋大炮','迪丽热巴','Lisa','琼斯','吴进炮'} set3 = {'Lisa','琼斯','王大炮','马大姐','李大爷','小孙'}
a. 求选课学生总共有多少人
set_s = set1 | set2 | set3 count = 0 for i in set_s: count += 1 print(count)
b. 求只选了第一个学科的人的数量和对应的名字
set_s = set1 - (set2 | set3) count = 0 for i in set_s: count += 1 print(count,set_s)
c. 求只选了一门学科的学生的数量和对应的名字
set_s = set1 ^ set2 ^set3 count = 0 for i in set_s: count += 1 print(count,set_s)
d. 求只选了两门学科的学生的数量和对应的名字
set_s = set1 ^ set2 ^ set3 set_d = set1 | set2 | set3 set_a = set_d - set_s count = 0 for i in set_a: count += 1 print(count,set_a)
e. 求选了三门学生的学生的数量和对应的名字
set_s = set1 & set2 & set3 count = 0 for i in set_s: count += 1 print(count,set_s)
-
获取列表中出现次数最多的元素
例如:nums = [1, 2, 3,1,4,2,1,3,7,3,3] —> 打印:3
nums = [1,2,2,1,3] --> 打印1、2
lis = [89, 69, 69, 92, 89, 69] dict1 = {} # 方法1 for i in lis: if i not in dict1: dict1.setdefault(i,lis.count(i)) for j in dict1: if dict1[j] == max(dict1.values()): print(j) # 方法2 for x in lis: if x not in dict1: dict1 = dict1.setdefault(x,0)+1 for y in dict1: if dict1[y] == max(dict1.values()): print(y)
-
实现给定一个日期,判断这个日期是今年第几天的程序(尝试)
例如:2022/12/31 --> 今年第365天;2022/1/1 --> 今年第1天
Year_1 = input('请输入一个日期(格式:2022/12/31):')
Year_1 = Year_1.split('/')
years = int(Year_1[0]) # 2022
months = int(Year_1[1]) # 09
days = int(Year_1[2]) # 28
month_days = [31,28,31,30,31,30,31,31,30,31,30,31]
if years % 400 == 0 or (years % 4 == 0 and years % 100 != 0):
month_days[1] = 29
if months == 1:
day_s = days
else:
day_s = sum(month_days[0:months-1]) + days
print(day_s)