Skip to content

搞英语 → 看世界

翻译英文优质信息和名人推特

Menu
  • 首页
  • 作者列表
  • 独立博客
  • 专业媒体
  • 名人推特
  • 邮件列表
  • 关于本站
Menu

将 Python 切换为匹配

Posted on 2025-02-27

switch-python-to-match-preview.jpeg

很长一段时间以来,我在许多脚本和项目中使用了传统的if/else语句。由于我的许多脚本最近都是用 Python 编写的,所以我又学到了一些我不知道的技巧。最近我需要使用switch语句对结果执行不同的操作。

当我开始学习 Python 3.5 左右的版本时,没有原生的switch命令,所以你必须创建自己的命令。这是一个例子。

 import datetime intDay = datetime . datetime . today ( ) . weekday ( ) days = [ "mon" , "tue" , "wed" , "thur" , "fri" , "sat" , "sun" ] def switch ( day ) : if day == "mon" : print ( "Monday, one day" ) elif day == "tue" : print ( "Tuesday, two day" ) elif day == "wed" : print ( "Wednesday, when? Huh? What day?" ) elif day == "thur" : print ( "Thursday! The third day!" ) elif day == "fri" : print ( "Friday! It's the weekend eve." ) elif day == "sat" : print ( "Saturday! Stop working" ) elif day == "sun" : print ( "Sunday funday!" ) else : print ( "Well it should be some day of the week..." ) print ( switch ( days [ intDay ] ) )

在示例中,我们使用datetime模块来获取当前工作日,并返回一个数字。然后,通过我们的days列表,我们可以根据数字对列表进行切片,并获得一个人性化的日期,即星期三的wed 。当使用datetime并获取工作日整数时,一周从星期一的位置0开始,星期日最后在7处。

 0 mon 1 tue 2 wed 3 thur 4 fri 5 sat 6 sun

这个switch功能很基本,设置了很多可重复的动作以供再次使用。然而,我需要创建函数,输入if\else所有双等号等等……另外,最后使用switch()函数可能会很复杂,因为将值放入函数中,并将其向下传递到进程中以获得最终输出。

发生了很多事情。这可以使用 Python 3.10 中发布的 Python 新的match语句来简化。

这是同样的例子。

 import datetime intDay = datetime . datetime . today ( ) . weekday ( ) days = [ "mon" , "tue" , "wed" , "thur" , "fri" , "sat" , "sun" ] match days [ intDay ] : case "mon" : print ( "Monday, one day" ) case "tue" : print ( "Tuesday, two day" ) case "wed" : print ( "Wednesday, when? Huh? What day?" ) case "thur" : print ( "Thursday! The third day!" ) case "fri" : print ( "Friday! It's the weekend eve." ) case "sat" : print ( "Saturday! Stop working" ) case "sun" : print ( "Sunday funday!" ) case _ : print ( "Well it should be some day of the week..." )

现在这很简单! match语句的另一个巧妙之处是您可以在case中包含标准条件。

 match intDay : case n if n <= 4 : print ( "It is a weekday!" ) case n if n <= 5 : print ( "It's the weekend" ) case _ : print ( "Unknown day of the week." )

您也可以提供多个案例!

 match intDay : case 0 | 1 | 2 | 3 | 4 : print ( "It is a weekday!" ) case 6 | 7 : print ( "It's the weekend" ) case _ : print ( "Unknown day of the week." )

您可以使用match语句并结合其他case条件(例如列表和复杂的推导式)来执行更多操作。

附加参考

特征 例子
基本匹配案例 匹配值:大小写模式:…
默认情况 案件 _: …
合并案例 案例模式1 |模式2:……
列表匹配 情况[x,y]:…
守卫条件 案例模式 if 条件:…

通过电子邮件回复

原文: https://claytonerrington.com/blog/switch-python-to-match/?utm_source=rss

本站文章系自动翻译,站长会周期检查,如果有不当内容,请点此留言,非常感谢。
  • Abhinav
  • Abigail Pain
  • Adam Fortuna
  • Alberto Gallego
  • Alex Wlchan
  • Answer.AI
  • Arne Bahlo
  • Ben Carlson
  • Ben Kuhn
  • Bert Hubert
  • Bits about Money
  • Brian Krebs
  • ByteByteGo
  • Chip Huyen
  • Chips and Cheese
  • Christopher Butler
  • Colin Percival
  • Cool Infographics
  • Dan Sinker
  • David Walsh
  • Dmitry Dolzhenko
  • Dustin Curtis
  • Elad Gil
  • Ellie Huxtable
  • Ethan Marcotte
  • Exponential View
  • FAIL Blog
  • Founder Weekly
  • Geoffrey Huntley
  • Geoffrey Litt
  • Greg Mankiw
  • Henrique Dias
  • Hypercritical
  • IEEE Spectrum
  • Investment Talk
  • Jaz
  • Jeff Geerling
  • Jonas Hietala
  • Josh Comeau
  • Lenny Rachitsky
  • Liz Danzico
  • Lou Plummer
  • Luke Wroblewski
  • Matt Baer
  • Matt Stoller
  • Matthias Endler
  • Mert Bulan
  • Mostly metrics
  • News Letter
  • NextDraft
  • Non_Interactive
  • Not Boring
  • One Useful Thing
  • Phil Eaton
  • Product Market Fit
  • Readwise
  • ReedyBear
  • Robert Heaton
  • Ruben Schade
  • Sage Economics
  • Sam Altman
  • Sam Rose
  • selfh.st
  • Shtetl-Optimized
  • Simon schreibt
  • Slashdot
  • Small Good Things
  • Taylor Troesh
  • Telegram Blog
  • The Macro Compass
  • The Pomp Letter
  • thesephist
  • Thinking Deep & Wide
  • Tim Kellogg
  • Understanding AI
  • 英文媒体
  • 英文推特
  • 英文独立博客
©2025 搞英语 → 看世界 | Design: Newspaperly WordPress Theme