是否有一种有效的 pythonic 方式来比较日期名称和 isoweekday() 的输出?
Is there an effiecient and pythonic way compare day name and output of isoweekday()?
我想将这些工作日的首字母缩写词转换为由 isoweekday() 函数关联的工作日数字。现在代码工作正常,但我正在寻找更有效的方法来做到这一点。
if day == "sun": day = "0"
if day == "mon": day = "1"
if day == "tue": day = "2"
if day == "wed": day = "3"
if day == "thu": day = "4"
if day == "fri": day = "5"
if day == "sat": day = "6"
使用字典代替条件:
weekday = {"sun": "0", "mon": "1", "tue": "2", "wed": "3", "thu": "4", "fri": "5", "sat": "6"}
并通过按键访问,例如:
day = weekday["sun"]
我想将这些工作日的首字母缩写词转换为由 isoweekday() 函数关联的工作日数字。现在代码工作正常,但我正在寻找更有效的方法来做到这一点。
if day == "sun": day = "0"
if day == "mon": day = "1"
if day == "tue": day = "2"
if day == "wed": day = "3"
if day == "thu": day = "4"
if day == "fri": day = "5"
if day == "sat": day = "6"
使用字典代替条件:
weekday = {"sun": "0", "mon": "1", "tue": "2", "wed": "3", "thu": "4", "fri": "5", "sat": "6"}
并通过按键访问,例如:
day = weekday["sun"]