最大值最小值

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""
找出列表中最大或最小的元素
"""


def main():
    fruits = ['grape', 'apple', 'strawberry', 'waxberry', 'pitaya']
    # 直接使用内置的max和min函数找出列表中最大和最小元素
    # print(max(fruits))
    # print(min(fruits))
    max_value = min_value = fruits[0]
    for index in range(1, len(fruits)):
        if fruits[index] > max_value:
            max_value = fruits[index]
        elif fruits[index] < min_value:
            min_value = fruits[index]
    print('Max:', max_value)
    print('Min:', min_value)


if __name__ == '__main__':
    main()

最大值次大值

设计一个函数返回传入的列表中最大和第二大的元素的值。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def max2(x):
    m1, m2 = (x[0], x[1]) if x[0] > x[1] else (x[1], x[0])
    for index in range(2, len(x)):
        if x[index] > m1:   # 最大值
            m2 = m1
            m1 = x[index]
        elif x[index] > m2: # 次大值
            m2 = x[index]
    return m1, m2

print(max2([1,2,3,4,5])) # (5, 4)

成绩表和平均分统计

录入5个学生3门课程的考试成绩,计算每个学生的平均分和每门课的平均分。

提醒大家在使用嵌套列表时应该避开的坑!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
录入5个学生3门课程的考试成绩
计算每个学生的平均分和每门课的平均分
"""
names = ['关羽', '张飞', '赵云', '马超', '黄忠']
courses = ['语文', '数学', '英语']
# 用生成式创建嵌套的列表保存5个学生3门课程的成绩
scores = [[0] * len(courses) for _ in range(len(names))]
# 录入数据
for i, name in enumerate(names):
    print(f'请输入{name}的成绩 ===>')
    for j, course in enumerate(courses):
        scores[i][j] = float(input(f'{course}: '))
print()
print('-' * 5, '学生平均成绩', '-' * 5)
# 计算每个人的平均成绩
for index, name in enumerate(names):
    avg_score = sum(scores[index]) / len(courses)
    print(f'{name}的平均成绩为: {avg_score:.1f}分')
print()
print('-' * 5, '课程平均成绩', '-' * 5)
# 计算每门课的平均成绩
for index, course in enumerate(courses):
    # 用生成式从scores中取出指定的列创建新列表
    # 因为scores列表是规律的所以直接取出相同位置的就可以
    curr_course_scores = [score[index] for score in scores]
    # 每一轮 score 其实是 scores 的 元素,就是一个列表
    # score:[1.0, 2.0, 3.0],[4.0, 5.0, 6.0]...
    # 然后取出score[index]存入到新的列表curr_course_scores中
    # 即为各科对应的成绩
    avg_score = sum(curr_course_scores) / len(names)
    print(f'{course}的平均成绩为:{avg_score:.1f}分')
    
"""
[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [11.0, 12.0, 13.0], [14.0, 15.0, 16.0]]
----- 学生平均成绩 -----
关羽的平均成绩为:2.0分
张飞的平均成绩为:5.0分
赵云的平均成绩为:8.0分
马超的平均成绩为:12.0分
黄忠的平均成绩为:15.0分

----- 课程平均成绩 -----
[1.0, 4.0, 7.0, 11.0, 14.0]
语文的平均成绩为:7.4分
[2.0, 5.0, 8.0, 12.0, 15.0]
数学的平均成绩为:8.4分
[3.0, 6.0, 9.0, 13.0, 16.0]
英语的平均成绩为:9.4分
v"""

上面对列表进行遍历的时候,使用了enumerate函数,这个函数非常有用。我们之前讲过循环遍历列表的两种方法,一种是通过索引循环遍历,一种是直接遍历列表元素。通过enumerate处理后的列表在循环遍历时会取到一个二元组,解包之后第一个值是索引,第二个值是元素,下面是一个简单的对比。

1
2
3
4
5
6
7
items = ['Python', 'Java', 'Go', 'Swift']

for index in range(len(items)):
    print(f'{index}: {items[index]}')

for index, item in enumerate(items):
    print(f'{index}: {item}')

指定日期是这一年的第几天

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
计算指定的年月日是这一年的第几天
"""
def is_leap_year(year):
    """判断指定的年份是不是闰年,平年返回False,闰年返回True"""
    return year % 4 == 0 and year % 100 != 0 or year % 400 == 0


def which_day(year, month, date):
    """计算传入的日期是这一年的第几天
    :param year: 年
    :param month: 月
    :param date: 日
    """
    # 用嵌套的列表保存平年和闰年每个月的天数
    days_of_month = [
        [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
        [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    ]
    # 布尔值False和True可以转换成整数0和1,因此
    # 平年会选中嵌套列表中的第一个列表(2月是28天)
    # 闰年会选中嵌套列表中的第二个列表(2月是29天)
    days = days_of_month[is_leap_year(year)]
    total = 0
    for index in range(month - 1):
        total += days[index]
    return total + date


print(which_day(1980, 11, 28))    # 333
print(which_day(1981, 12, 31))    # 365
print(which_day(2018, 1, 1))      # 1
print(which_day(2016, 3, 1))      # 61

实现双色球随机选号

双色球属乐透型彩票范畴,由中国福利彩票发行管理中心统一组织发行,在全国范围内销售。红球号码范围为01~33,蓝球号码范围为01~16。双色球每期从33个红球中开出6个号码,从16个蓝球中开出1个号码作为中奖号码,双色球玩法即是竞猜开奖号码的6个红球号码和1个蓝球号码。

这个题目的思路是用一个列表保存红色球的号码,然后通过random模块的sample函数实现无放回抽样,这样就可以抽中6个不重复的红色球号码。红色球需要排序,可以使用列表的sort方法,显示的时候一位数前面需要做补0的操作,可以用字符串格式化的方式来处理。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
双色球随机选号
"""
from random import randint, sample


def display(balls):
    """输出列表中的双色球号码"""
    for index, ball in enumerate(balls):
        if index == len(balls) - 1:
            print('|', end=' ')
        print(f'{ball:0>2d}', end=' ')
    print()


def random_select():
    """随机选择一组号码"""
        # 用生成式生成1到33号的红色球
    red_balls = [x for x in range(1, 34)]
    # 通过无放回随机抽样的方式选中6个红色球
    selected_balls = sample(red_balls, 6)
    # 对红色球进行排序
    selected_balls.sort()
    # 用1到16的随机数表示选中的蓝色球并追加到列表中
    selected_balls.append(randint(1, 16))
    return selected_balls


n = int(input('机选几注: '))
for _ in range(n):
    display(random_select())

幸运的女人

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

上面这个问题其实就是著名的约瑟夫环问题。我们可以通过一个列表来保存这30个人是死是活的状态,例如用布尔值True表示活着的人,用False表示被扔到海里的人。最开始的时候列表中的30个元素都是True,然后我们通过循环的方式去执行报数,找到要扔到海里的人并将对应的列表元素标记为False,循环会执行到将列表中的15个元素标记为False,循环的过程中,列表的索引始终在029的范围,超过29就回到0,这样刚好可以形成一个闭环。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
"""
幸运的女人(约瑟夫环问题)
"""
persons = [True] * 30
# counter - 扔到海里的人数
# index - 访问列表的索引
# number - 报数的数字
counter, index, number = 0, 0, 0
while counter < 15:
    if persons[index]:
        number += 1
        if number == 9:
            persons[index] = False
            counter += 1
            number = 0
    index += 1
    index %= 30
for person in persons:
    print('女' if person else '男', end='')

绘制杨辉三角形

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def main():
    num = int(input('Number of rows: '))
    yh = [[]] * num
    # [[], [], [], []]
    for row in range(len(yh)):
        yh[row] = [None] * (row + 1)
        # [None, None, None, None]
        for col in range(len(yh[row])):
            if col == 0 or col == row:
                yh[row][col] = 1
            else:
                yh[row][col] = yh[row - 1][col] + yh[row - 1][col - 1]
                # 上一行的这一位与前一位的和
            print(yh[row][col], end='\t')
        print()


if __name__ == '__main__':
    main()
"""
Number of rows: 4
1	
1	1	
1	2	1	
1	3	3	1	
"""

井字棋

没看懂。。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os


def print_board(board):
    print(board['TL'] + '|' + board['TM'] + '|' + board['TR'])
    print('-+-+-')
    print(board['ML'] + '|' + board['MM'] + '|' + board['MR'])
    print('-+-+-')
    print(board['BL'] + '|' + board['BM'] + '|' + board['BR'])

def main():
    init_board = {
        'TL': ' ', 'TM': ' ', 'TR': ' ',
        'ML': ' ', 'MM': ' ', 'MR': ' ',
        'BL': ' ', 'BM': ' ', 'BR': ' '
    }
    begin = True
    while begin:
        curr_board = init_board.copy()
        begin = False
        turn = 'x'
        counter = 0
        os.system('cls')
        print_board(curr_board)
        while counter < 9:
            move = input('轮到%s走棋, 请输入位置: ' % turn)
            if curr_board[move] == ' ':
                counter += 1
                curr_board[move] = turn
                if turn == 'x':
                    turn = 'o'
                else:
                    turn = 'x'
            os.system('cls')
            print_board(curr_board)
        choice = input('再玩一局?(yes|no)')
        begin = choice == 'yes'

if __name__ == '__main__':
    main()

说明: 最后这个案例来自《Python编程快速上手:让繁琐工作自动化》一书(这本书对有编程基础想迅速使用Python将日常工作自动化的人来说还是不错的选择),对代码做了一点点的调整。

斐波那契数列

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13

def main():
    f = [1 , 1]
    for i in range(2, 20):
        f += [f[i - 1] + f[i - 2]]
        # f.append(f[i - 1] + f[i - 2])
    for val in f:
        print(val, end=' ')


if __name__ == '__main__':
    main()