使用python编写一段程序,程序要求:“输入一个日期,例如‘’20221001‘,输出该日期对应的节假日(也可能是周六或周末),如果不是节日就输出工作日’”。
这个我刚好做过一个类似的项目
我先定义了一个函数,用于更新文本:
def is_workdate(self, state: bool = True, weekdate: str = ""):
if state:
self.worker_label.config(text="[星期{}-工作日]".format(weekdate))
else:
self.worker_label.config(text="[星期{}-非工作日]".format(weekdate))
然后调用的时候通过下面这个函数获取是否工作日和周几:
from workalendar.asia import China
def is_workday(self):
cal = China()
date = datetime.datetime.now().date()
weekdays = ["一", "二", "三", "四", "五", "六", "日"]
return cal.is_working_day(date), weekdays[date.weekday()]
当然,这里的workalendar是要单独pip安装的,这个库每年都会更新一次
【相关推荐】
代码如下:
from scipy.optimize import minimize
import numpy as np
e = 1e-10 # 非常接近0的值
fun = lambda x : 8 * (x[0] * x[1] * x[2]) # 约束函数f(x,y,z) =8 *x*y*z
cons = ({'type': 'eq', 'fun': lambda x: x[0]**2+ x[1]**2+ x[2]**2 - 1}, # x^2 + y^2 + z^2=1
{'type': 'ineq', 'fun': lambda x: x[0] - e}, # x>=e,即 x > 0
{'type': 'ineq', 'fun': lambda x: x[1] - e},
{'type': 'ineq', 'fun': lambda x: x[2] - e}
)
x0 = np.array((1.0, 1.0, 1.0)) # 设置初始值
res = minimize(fun, x0, method='SLSQP', constraints=cons)
print('最大值:',res.fun)
print('最优解:',res.x)
print('迭代终止是否成功:', res.success)
print('迭代终止原因:', res.message)
在 jupyter notebook 中运行
运行结果: