1.处理日期时间
from datetime import datetime as dt
now=dt.now()
print(now)
import time
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
print (time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))
cday = dt.strptime('2018-8-08 18:08:08', '%Y-%m-%d %H:%M:%S')
print(cday)
print("今天是{0:%Y}的第{0:%j}天。".format(now))
print("今年还有70天")
2018
年
10
月
24
日星期三
23时18
分
50
秒
2018-8-08 18:08:08
今年是2018年的第297天
今年还有68天
2.问题
def sq(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 c sq(10)
[
0
,
126
,
1004
,
3384
,
8016
,
15650
,
27036
,
42924
,
64064
,
91206
]
import numpy as np def py(n): a = np.arange(n) b = np.arange(0,5*n,5) c = a**2 + b**3 return c py(10)
array([
0
,
126
,
1004
,
3384
,
8016
,
15650
,
27036
,
42924
,
64064
,
91206
], dtype
=
int32)
from datetime import timedelta,datetime t1 = datetime.now() sq(10000) t2 = datetime.now() print(t2-t1) t3 = datetime.now() py(10000) t4 = datetime.now() print(t4 - t3)
0
:
00
:
00.009001
0
:
00
:
00.001000
3.尝试把a,b定义为三层嵌套列表和三维数组,求相对应元素的ai2+bi3
对比两种数据类型处理方法及效率的不同。
def Sum(n): #定义一个函数(注意:格式对齐,否则会出错) a=list(range(n)) b=list(range(0,50000*n,5)) c=[] for i in range(len(a)): c.append(a[i]**2+b[i]**3) return c print(Sum(20)) import numpy as py def pySum(n): a=py.array(range(n)) b=py.array(range(0,500000*n,n)) c=[] for i in range(len(a)): c.append(a[i]**2+b[i]**3) return c print(pySum(20)) import datetime def new4(): now1=datetime.datetime.now() Sum(30000) now2=datetime.datetime.now() pySum(30000) now3=datetime.datetime.now() print("sum执行时间(30W数据):" , now2-now1,"\npysum数组执行时间(30W数据):" , now3-now2) new4()