python计算相差几天

def check_year(year): if (year % 4) == 0:   if (year % 100) == 0:       if (year % 400) == 0:           return 1   # 整百年能被400整除的是闰年       else:          return 0   else:      return 1      # 非整百年能被4整除的为闰年 else:   return 0def deal_time(time):    year=time.split(‘-‘)[0]    month=time.split(‘-‘)[1]    days=time.split(‘-‘)[2].split(‘ ‘)[0]    hours=time.split(‘ ‘)[1].split(‘:‘)[0]    return int(year),int(month),int(days),int(hours)def day_hour_differ(time1,time2):    time1=str(time1)    time2=str(time2)    days=0    year,month1,days1,hours1=deal_time(time1)    year1,month2,days2,hours2=deal_time(time2)    if (hours2 - hours1)<0:        hours2=hours2+24        days2=days2-1        hour=hours2-hours1    else:        hour=hours2-hours1    if days2 - days1<0:      if month2 in [1,3,5,7,8,10,12]:        days2=days2+31        days=days2-days1        month2=month2-1      elif month2 in [4,6,9,11]:

        days2=days2+30        days=days2-days1        month2=month2-1      elif check_year(year)==1:        days2=days2+29        days=days2-days1        month2=month2-1      elif check_year(year)==0:        days2=days2+28        days=days2-days1        month2=month2-1    else:       days=days2-days1    return days,hour
时间: 2024-07-30 13:45:07

python计算相差几天的相关文章

Python计算斐波那契数列

利用Python计算第一个达到一百万位数的斐波那契数列各位数之和 结果为4501552 以下是我用到的代码,不是中间需要一些人工操作来加快收敛性,有兴趣读者可以写代码加快收敛 首先执行这个,可以大致确定一百万个数所在斐波那契序列的位置 i=1 j=1 k=i+j count=3 while count<4850000: i=j j=k k=i+j count+=1 result=str(k) print('k长度') k_len=len(result) print(k_len) sum=0 fo

数学之路-python计算实战(18)-机器视觉-滤波去噪(双边滤波与高斯滤波 )

高斯滤波就是对整幅图像进行加权平均的过程,每一个像素点的值,都由其本身和邻域内的其他像素值经过加权平均后得到.高斯滤波的具体操作是:用一个模板(或称卷积.掩模)扫描图像中的每一个像素,用模板确定的邻域内像素的加权平均灰度值去替代模板中心像素点的值. #滤波去噪 lbimg=cv2.GaussianBlur(newimg,(3,3),1.8) cv2.imshow('src',newimg) cv2.imshow('dst',lbimg) cv2.waitKey() cv2.destroyAllW

数学之路-python计算实战(16)-机器视觉-滤波去噪(邻域平均法滤波)

# -*- coding: utf-8 -*- #code:[email protected] #邻域平均法滤波,半径为2 import cv2 import numpy as np fn="test3.jpg" myimg=cv2.imread(fn) img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY) #加上椒盐噪声 param=20 #灰阶范围 w=img.shape[1] h=img.shape[0] newimg=np.array(img)

python计算均值方差

用Python求均值与方差,可以自己写,也可以借助于numpy,不过到底哪个快一点呢? 我做了个实验,首先生成9百万个样本: ? 1 2 3 nlist=range(0,9000000) nlist=[float(i)/1000000 for i in nlist] N=len(nlist) 第二行是为了让样本小一点,否则从1加到9百万会溢出的. 自己实现,遍历数组来求均值方差: ? 1 2 3 4 5 6 7 sum1=0.0 sum2=0.0 for i in range(N):     s

python计算auc指标

本文和大家分享的主要是python计算auc指标相关内容,一起来看看吧,希望对大家学习python有所帮助. 1.安装scikit-learn 1.1Scikit-learn 依赖 · Python (>= 2.6 or >= 3.3), · NumPy (>= 1.6.1), · SciPy (>= 0.9). 分别查看上述三个依赖的版本, python -V        结果:Python 2.7.3 python -c 'import scipy; print scipy.

python 计算校验和

校验和是经常使用的,这里简单的列了一个针对按字节计算累加和的代码片段.其实,这种累加和的计算,将字节翻译为无符号整数和带符号整数,结果是一样的. 使用python计算校验和时记住做截断就可以了. 这里仅仅是作为一个代码样本,权作标记,直接上代码 ''' Created on 2014年9月4日 @author: lenovo ''' import random ''' 实际计算校验和时,解释为无符号整数还是带符号整数,结果必然是一样的.因为基于补码方式存储,计算加法时都是按位加,然后该进位的就进

数学之路-python计算实战(21)-机器视觉-拉普拉斯线性滤波

拉普拉斯线性滤波,.边缘检测   Laplacian Calculates the Laplacian of an image. C++: void Laplacian(InputArray src, OutputArray dst, int ddepth, int ksize=1, double scale=1, double delta=0, int borderType=BORDER_DEFAULT ) Python: cv2.Laplacian(src, ddepth[, dst[, k

数学之路-python计算实战(22)-机器视觉-sobel非线性滤波

sobel非线性滤波,采用梯度模的近似方式 Sobel Calculates the first, second, third, or mixed image derivatives using an extended Sobel operator. C++: void Sobel(InputArray src, OutputArray dst, int ddepth, int dx, int dy, intksize=3, double scale=1, double delta=0, int

数学之路-python计算实战(11)-机器视觉-图像增强

在计算机领域中,灰度(Gray scale)数字图像是每个像素只有一个采样颜色的图像.这类图像通常显示为从最暗黑色到最亮的白色的灰度,尽管理论上这个采样可以任何颜色的不同深浅,甚至可以是不同亮度上的不同颜色.灰度图像与黑白图像不同,在计算机图像领域中黑白图像只有黑白两种颜色,灰度图像在黑色与白色之间还有许多级的颜色深度.用于显示的灰度图像通常用每个采样像素8 bits的非线性尺度来保存,这样可以有256种灰度(8bits就是2的8次方=256).这种精度刚刚能够避免可见的条带失真,并且非常易于编