day9集合作业

  1. 用三个集合表示三门学科的选课学生姓名(一个学生可以同时选多门课)

    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)
    
  2. 获取列表中出现次数最多的元素

    例如: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)
    
  3. 实现给定一个日期,判断这个日期是今年第几天的程序(尝试

    例如: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, '天')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值