从if-elif-match改为match-case可能是嵌套的。

huangapple go评论61阅读模式
英文:

Change from if-elif-match to match-case maybe nested

问题

在安装了Python 3.10后,我尝试将所有的if-elif-else,其中您只看到了一小部分,转换为match-case。在第一个if中,我没有遇到任何问题,但在切换到第二个if时,我遇到了两个问题:

  • case中只期望OR,而不是AND
  • 不可能做嵌套的match-case或嵌套的case,也不能使用if

是否有办法解决这个“问题”,或者我应该保留旧的代码?

def strip(self, value, dow, day, holiday):
   if day in holiday:
     match value:
       case 'M': retval = '10-14:30'
       case 'P': retval = '14:30-19'
       case 'E': retval = 'Mos 10-14:30'
       case 'D': retval = 'Mos 14:30-19'
       case 'Y': retval = 'Mos 10-19'
       case 'Z': retval = 'Mix 10-14:30-19'
       case _  : pass
                
   elif ((dow == 'tue') or (dow == 'wed')) and ((value == 'P') or (value == 'F') or (value == 'B') or (value == 'D')):retval = "14-19"
   elif ((dow == 'thu')) and ((value == 'M') or (value == 'G') or (value == 'A') or (value == 'E')):retval = "14-17"
   elif ((dow == 'thu')) and ((value == 'P') or (value == 'F') or (value == 'B') or (value == 'D')):retval = "17-20"
   elif ((dow == 'fri') or (dow == 'sat')) and ((value == 'P') or (value == 'F') or (value == 'B') or (value == 'D')):retval = "14:30-19"
   ... 其他许多
英文:

After installing python3.10 I tried to transform all if-elif-else, of which you see only a small part, into match-case. While for the first if I didn't have any problems when I switched to the second one I ran into two problems:

  • in the case only the OR is expected but not the AND

  • it is not possible to do a nested match-case or a nested 'case' nor an if
    Is there a way to solve the "problem" or should I keep the old code?

      def strip(self, value, dow, day, holiday):
         if day in holiday:
           match value:
             case 'M': retval = '10-14:30'
             case 'P': retval = '14:30-19'
             case 'E': retval = 'Mos 10-14:30'
             case 'D': retval = 'Mos 14:30-19'
             case 'Y': retval = 'Mos 10-19'
             case 'Z': retval = 'Mix 10-14:30-19'
             case _  : pass
    
         elif ((dow == 'tue') or (dow == 'wed')) and ((value == 'P') or (value == 'F') or (value == 'B') or (value == 'D')):retval = "14-19"
         elif ((dow == 'thu')) and ((value == 'M') or (value == 'G') or (value == 'A') or (value == 'E')):retval = "14-17"
         elif ((dow == 'thu')) and ((value == 'P') or (value == 'F') or (value == 'B') or (value == 'D')):retval = "17-20"
         elif ((dow == 'fri') or (dow == 'sat')) and ((value == 'P') or (value == 'F') or (value == 'B') or (value == 'D')):retval = "14:30-19"
         ... many others                 
    

答案1

得分: 1

你最好重新编写你的条件。它们太复杂了。你可以创建一个带有键-返回值和值-条件的字典,然后循环遍历这个字典以获取结果。

def strip(value, dow, day, holiday):
    retval = ""
    conditions = {
        "14-19": dow in ['tue','wed'] and value in ['P', 'F', 'B', 'D'],
        "14-17": (dow == 'thu' and value == 'M') or (value in ['G', 'A', 'E']),
        "17-20": (dow == 'thu' and value == 'P') or (value in ['F', 'B', 'D']),
        "14:30-19": dow in ['fri', 'sat'] and value in ['P', 'F', 'B', 'D'],
    }
    if day in holiday:
        match value:
            case 'M': retval = '10-14:30'
            case 'P': retval = '14:30-19'
            case 'E': retval = 'Mos 10-14:30'
            case 'D': retval = 'Mos 14:30-19'
            case 'Y': retval = 'Mos 10-19'
            case 'Z': retval = 'Mix 10-14:30-19'
            case _  : pass
    else:
        for key, value in conditions.items():
            if value:
                retval = key
                break
    
    return retval
英文:

You better to rewrite your conditions. They are to complicated. You can create dictionary with key - retval and value - condition. And loop through this dict to get result.

def strip(value, dow, day, holiday):
    retval = ""
    conditions = {
        "14-19": dow in ['tue','wed'] and value in ['P', 'F', 'B', 'D'],
        "14-17": (dow == 'thu' and value == 'M') or (value in ['G', 'A', 'E']),
        "17-20": (dow == 'thu' and value == 'P') or (value in ['F', 'B', 'D']),
        "14:30-19": dow in ['fri', 'sat'] and value in ['P', 'F',  'B', 'D'],
    }
    if day in holiday:
        match value:
            case 'M': retval = '10-14:30'
            case 'P': retval = '14:30-19'
            case 'E': retval = 'Mos 10-14:30'
            case 'D': retval = 'Mos 14:30-19'
            case 'Y': retval = 'Mos 10-19'
            case 'Z': retval = 'Mix 10-14:30-19'
            case _  : pass
    else:
        for key, value in conditions.items():
            if value:
                retval = key
                break
    
    return retval

huangapple
  • 本文由 发表于 2023年2月24日 00:48:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定