一、处理日期时间
取系统时间
转换成‘2017年9月30日星期六10时28分56秒’格式字符串
’2018-10-25 22:00‘转换成一个日期时间变量
计算两者的间隔
import datetimeprint(datetime.datetime.now())print("--------------------------------")from datetime import datetime,timedeltanow = datetime.now()print(now)print("--------------------------------")dt = datetime(2019,10,22,8,59)print(dt)print("--------------------------------")cday=datetime.strptime('2015-6-1 18:19:59','%Y-%m-%d %H:%M:%S')print(cday)print("--------------------------------")now1 =now.strftime('%a, %b %d %H:%M')print(now1)print("--------------------------------")print('今天是{0:%y}年的第{0:%j}天。'.format(now))print("--------------------------------")print(dt-now)print("--------------------------------")
二、问题:
- 数列:
- a = a1,a2,a3,·····,an
- b = b1,b2,b3,·····,bn
- 求:
- c = a12+b13,a22+b23,a32+b33,·····+an2+bn3
1.用列表+循环实现,并包装成函数
2.用numpy实现,并包装成函数
3.对比两种方法实现的效率,给定一个较大的参数n,用运行函数前后的timedelta表示。
import numpy as pyfrom datetime import datetimedef listSum(n): a=list(range(n)) b=list(range(0,5*n,5)) c=[] for i in range(len(a)): c.append(a[i]**2+b[i]**3) return cdef numpySum(n): a=py.arange(n) b=py.arange(0,5*n,5) c=a**2+b**3 return cnow1=datetime.now()print(listSum(1000000))now2=datetime.now()print(now2-now1)now3=datetime.now()print(numpySum(1000000))now4=datetime.now()print(now4-now3)