-
用三个集合表示三门学科的选课学生姓名(一个学生可以同时选多门课)
math = {'小明', '小马', '小红', '小贝', '小李', '小华', '小东', '小南'} chinese = {'小马', '小红', '小贝', '小周', '小张', '小明', '小王'} English = {'小李', '小华', '小贝', '小周', '小赵', '小钱', '小南'}
a. 求选课学生总共有多少人
s = math | chinese | English print(len(s))
b. 求只选了第一个学科的人的数量和对应的名字
s1 = math - chinese - English print(len(s1), s1)
c. 求只选了一门学科的学生的数量和对应的名字
s2 =(math - chinese - English) | (chinese - math - English) |(English - chinese - math) print(len(s2), s2)
d. 求只选了两门学科的学生的数量和对应的名字
s = math | chinese | English # 只选了一门的 s2 =(math - chinese - English) | (chinese - math - English) |(English - chinese - math) # 选了三门的 s4 = math & chinese & English s5 = s -s2 -s4 print(len(s5), s5)
e. 求选了三门学生的学生的数量和对应的名字
s4 = math & chinese & English print(len(s4), s4)
-
获取列表中出现次数最多的元素
例如:nums = [1, 2, 3,1,4,2,1,3,7,3,3] —> 打印:3
nums = [1,2,2,1,3] --> 打印1、2
nums = [1,2,2,1,3] s = set(nums) data1={} for i in s: data1[i]=nums.count(i) print(data1) list1=[] for i in data1: list1.append(data1[i]) for i in data1: if data1[i] == max(list1): print(i)
-
实现给定一个日期,判断这个日期是今年第几天的程序(尝试)
例如:2022/12/31 --> 今年第365天;2022/1/1 --> 今年第1天
str1='2022/12/31'
year = int(str1[:4:])
month = int(str1[5:7:])
day = int(str1[8::])
data1 = {'1': 31, '2': 30, '3': 31, '4': 30, '5': 31, '6': 30, '7': 31,
'8': 31, '9': 30, '10': 31, '11': 30, '12': 31}
# 二月份多少天
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
data1['2'] = 29
else:
data1['2'] = 28
sum1 = 0
for i in range(month-1):
sum1 += data1[str(i+1)]
print('今年第', sum1 + day, '天')