Python计算已经过去多少个周末的方法

yipeiwu_com6年前Python基础

本文实例讲述了Python计算已经过去多少个周末的方法。分享给大家供大家参考。具体如下:

def weekends_between(d1,d2):
  days_between = (d2-d1).days
  weekends, leftover = divmod(days_between,7)
  if leftover:
    start_day = (d2-timedelta(leftover)).isoweekday()
    end_day = start_day+leftover
    if start_day<=6 and end_day>6:
      weekends +=.5
    if start_day<=7 and end_day>7:
      weekends +=.5
  return weekends

使用方法:

复制代码 代码如下:
weekends_between(date(2004,10,1),date(2004,10,10))

希望本文所述对大家的Python程序设计有所帮助。

相关文章

tensorflow-gpu安装的常见问题及解决方案

tensorflow-gpu安装的常见问题及解决方案

装tensorflow-gpu的时候经常遇到问题,自己装过几次,经常遇到相同或者类似的问题,所以打算记录一下,也希望对其他人有所帮助 基本信息 tensorflow-gpu p...

pytorch实现用CNN和LSTM对文本进行分类方式

model.py: #!/usr/bin/python # -*- coding: utf-8 -*- import torch from torch import nn imp...

Python迭代器模块itertools使用原理解析

这篇文章主要介绍了Python迭代器模块itertools使用原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 介绍 今天介绍...

Python version 2.7 required, which was not found in the registry

Python version 2.7 required, which was not found in the registry

安装PIL库的时候,直接提示:Python version 2.7 required, which was not found in the registry。 如图: 大意是说找不到...

Python3基础之输入和输出实例分析

通常来说,一个Python程序可以从键盘读取输入,也可以从文件读取输入;而程序的结果可以输出到屏幕上,也可以保存到文件中便于以后使用。本文就来介绍Python中最基本的I/O函数。 一、...