day06 列表应用

列表应用

1. 双色彩
# 方法一
n = eval(input('请输入需要多少组随机号码'))
count = 0
while count < n:
	nums = []
     while (len(nums)) < 6:
         red = random.randint(1, 33)
         if red not in nums:
             nums.append(red)
     nums.sort()
     nums.append(random.randint(1, 16))
     count += 1
     print(nums)

# 方法二
 n = int(input('机选几注:'))
 for _ in range(n):
     red_balls = [x for x in range(1, 34)]
     # 不放回随机抽样
     selected_balls = random.sample(red_balls, 6)

     # selected_balls = []
     # for _ in range(6):
     #     index = random.randint(0, len(red_balls) - 1)
     #     selected_balls.append(red_balls.pop(index))

     selected_balls.sort(reverse=True)  # 逆序 不加就从小到大
     selected_balls.append(random.randint(1, 16))
     for ball in selected_balls:
         print(f'{ball:0>2d}', end=' ')  # d 整型,f浮点型,s字符型  >在数字前面补0,< 在数字后面补0
     print()  # 常规换行操作 
     #有网的时候shift+f1查用法
2. Josephu环问题

有15个男人和15个女人乘船在海上遇险,为了让一部分人活下来,
不得不将其中15个人扔到海里,有个人想了个办法让大家围成一个圈,
由某个人开始从1报数,报到9的人就扔到海里面,他后面的人接着从1开始报数,
报到9的人继续扔到海里面,直到将15个人扔到海里。
最后15个女人都幸免于难,15个男人都被扔到了海里。
问这些人最开始是怎么站的,哪些位置是男人,哪些位置是女人。

peoples = [True] * 30
index, count, counter = 0, 0, 0
while counter != 15:
    if peoples[index]:
        count += 1
        if count == 9:
            peoples[index] = False
            counter += 1
            count = 0
    index += 1
    index %= 30

for people in peoples:
    # 三元条件运算
    print('女' if people else '男', end='')
    # if 后面的条件如果成立,取if前面的值
    # if 后面的条件如果不成立,取else后面的值
3. 列表嵌套

使用嵌套列表保存五个学生三门课程的成绩,计算每个学生和每门课程的平均分

name = [['张三'], ['丽丝'], ['李四'], ['王五'], ['小二']]
for x in range(len(name)):
    for y in range(len(subject)):
        # 二维数组
        name[x].append(subject[y][x])
        average = round((subject0[x] + subject1[x] + subject2[x]) / 3, 2)
    print(name[x][0], f'的平均成绩是{average}')
for i in range(len(subject)):
    print(f'subject{i}平均成绩是{sum(subject[i]) / len(subject[i])}')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值