当前位置:首页>python>Python 基础----模式匹配

Python 基础----模式匹配

  • 2026-08-22 16:22:30
Python 基础----模式匹配

使用match语句时,我们依次用case xxx匹配,并且可以在最后(且仅能在最后)加一个case _表示“任意值”,代码较if ... elif ... else ...更易读。

score = 'B'match score:    case 'A':        print('score is A.')    case 'B':        print('score is B.')    case 'C':        print('score is C.')    case _: # _表示匹配到其他任何情况        print('score is ???.')

复杂匹配

match语句除了可以匹配简单的单个值外,还可以匹配多个值、匹配一定范围,并且把匹配后的值绑定到变量:

age = 15match age:    case x if x < 10:        print(f'< 10 years old: {x}')    case 10:        print('10 years old.')    case 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18:        print('11~18 years old.')    case 19:        print('19 years old.')    case _:        print('not sure.')

匹配列表

match语句还可以匹配列表,功能非常强大。

我们假设用户输入了一个命令,用args = ['gcc', 'hello.c']存储,下面的代码演示了如何用match匹配来解析这个列表:

args = ['gcc', 'hello.c', 'world.c']# args = ['clean']# args = ['gcc']match args:    # 如果仅出现gcc,报错:    case ['gcc']:        print('gcc: missing source file(s).')    # 出现gcc,且至少指定了一个文件:    case ['gcc', file1, *files]:        print('gcc compile: ' + file1 + ', ' + ', '.join(files))    # 仅出现clean:    case ['clean']:        print('clean')    case _:        print('invalid command.')

第一个case ['gcc']表示列表仅有'gcc'一个字符串,没有指定文件名,报错;

第二个case ['gcc', file1, *files]表示列表第一个字符串是'gcc',第二个字符串绑定到变量file1,后面的任意个字符串绑定到*files

第三个case ['clean']表示列表仅有'clean'一个字符串;

最后一个case _表示其他所有情况。

最新文章

随机文章