英文:
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 anif
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论