import math
math.sin(0.5) #求0.5的正弦
math.pow(x, y) # x 的 y 次幂
math.sqrt(x) #x 的算术平方根,也就是正数的平方根
calendar闰年判断
>>> calendar.isleap(2016)
True
>>> calendar.isleap(2015)
False
itertoolscycle()
无限迭代器 cycle('ABCD') --> A B C D A B C D ...
>>> import itertools >>> x = 'Private Key' >>> y = itertools.cycle(x) #循环遍历序列中的元素 >>> for i in range(20): print(next(y), end=',') P,r,i,v,a,t,e, ,K,e,y,P,r,i,v,a,t,e, ,K, >>> for i in range(5): print(next(y), end=',') e,y,P,r,i,
根据一个序列的值对另一个序列进行过滤的函数compress()
>>> x = range(1, 20) >>> y = (1,0)*9+(1,) >>> y (1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1) >>> list(itertools.compress(x, y)) #根据一个序列的值对另一个序列进行过滤 [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
根据函数返回值对序列进行分组的函数groupby()
groupby(iterable[, keyfunc]) 按照keyfunc函数对序列每个元素执行后的结果分组(每个分组是一个迭代器), 返回这些分组的迭代器
>>> def group(v): if v>10: return 'greater than 10' elif v<5: return 'less than 5' else: return 'between 5 and 10' >>> x = range(20) >>> y = itertools.groupby(x, group) #根据函数返回值对序列元素进行分组 >>> for k, v in y: print(k, ':', list(v)) less than 5 : [0, 1, 2, 3, 4] between 5 and 10 : [5, 6, 7, 8, 9, 10] greater than 10 : [11, 12, 13, 14, 15, 16, 17, 18, 19]



