繁体   English   中英

如何简化具有许多 if-else 语句的 python 代码?

[英]How to simplify python code that has many if-else statements?

所以我有这段代码,但我不想使用很多 if-else 条件,我想知道我是否可以简化它。 有什么想法吗? 我可以为此使用循环吗?

    if dh1['date1'][0].strftime("%A") == 'Monday':
        df=df=pd.concat([dh1,dh2.tail(84)])
        df=df.sort_values(['date1','hr1'])
        df=df.reset_index()
        df=df.drop('index', 1)
    elif dh1['date1'][0].strftime("%A") == 'Tuesday':
        df=df=pd.concat([dh1,dh2.tail(96)])
        df=df.sort_values(['date1','hr1'])
        df=df.reset_index()
        df=df.drop('index', 1)
    elif dh1['date1'][0].strftime("%A") == 'Wednesday':
        df=df=pd.concat([dh1,dh2.tail(108)])
        df=df.sort_values(['date1','hr1'])
        df=df.reset_index()
        df=df.drop('index', 1)
    elif dh1['date1'][0].strftime("%A") == 'Thursday':
        df=df=pd.concat([dh1,dh2.tail(120)])
        df=df.sort_values(['date1','hr1'])
        df=df.reset_index()
        df=df.drop('index', 1)
    elif dh1['date1'][0].strftime("%A") == 'Friday':
        df=df=pd.concat([dh1,dh2.tail(132)])
        df=df.sort_values(['date1','hr1'])
        df=df.reset_index()
        df=df.drop('index', 1)
    elif dh1['date1'][0].strftime("%A") == 'Saturday':
        df=df=pd.concat([dh1,dh2.tail(144)])
        df=df.sort_values(['date1','hr1'])
        df=df.reset_index()
        df=df.drop('index', 1)
    elif dh1['date1'][0].strftime("%A") == 'Sunday':
        df=df=pd.concat([dh1,dh2.tail(156)])
        df=df.sort_values(['date1','hr1'])
        df=df.reset_index()
        df=df.drop('index', 1)

似乎所有分支之间的唯一区别是传递给tail方法的参数。 此外,相邻日期的参数值之间的差异为12 ,因此可以计算为84 + 12 * weekday从星期一开始计算为0 如果确实如此,您可以像这样减少代码:

arg = 84 + dh1['date'][0].weekday() * 12
df=df=pd.concat([dh1,dh2.tail(arg)])
df=df.sort_values(['date1','hr1'])
df=df.reset_index()
df=df.drop('index', 1)

唯一的区别是df.tail参数,它取决于date1列。 每当这些情况出现时,您都会创建如下映射。

tail_day_map = {
    'Monday': 84,
    'Tuesday': 96,
    'Wednesday': 108,
    'Thursday': 120,
    'Friday': 132,
    'Saturday': 144,
    'Sunday': 156
}

def perform_action(df, tail_day_map):
    tail_number = tail_day_map[df['date1'][0].strftime("%A")]
    df = df.tail(tail_number)
    df = df.sort_values(['date1','hr1'])
    df = df.reset_index()
    df = df.drop('index', 1)
    return df

如果您能够使用 Python 3.10 或更高版本,那么使用 Match Case 怎么样? https://learnpython.com/blog/python-match-case-statement/

Match Case 可以简化您的代码并提供更多的可读性,但如第二个链接中所述,请谨慎处理案例的顺序,因为它可能会改变逻辑的行为。

否则,您可能可以使用其他流量控制策略: https://docs.python.org/3/tutorial/controlflow.html

dict = {
  "Monday": 84,
  "Tuesday": 96,
  ...
}

你可以在这里使用字典

第一的,
df=df=pd.concat([dh1,dh2.tail(156)])也许可以
df=pd.concat([dh1,dh2.tail(156)])
然后,

df=df.sort_values(['date1','hr1'])
df=df.reset_index()
df=df.drop('index', 1)

可以放出if条件。
那么对我来说,
我将在 out of if 条件下使用d = {"Monday":84,"Tuesday":96...}

weekday = dh1['date1'][0].strftime("%A")
df=pd.concat([dh1,dh2.tail(d[weekday])])

所以最终的代码如下:

d = {"Monday":84,"Tuesday":96...}
weekday = dh1['date1'][0].strftime("%A")
df=pd.concat([dh1,dh2.tail(d[weekday])])
df=df.sort_values(['date1','hr1'])
df=df.reset_index()
df=df.drop('index', 1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM