2019年1月8日
摘要: from functools import lru_cache import time from functools import wraps def clock(func): @wraps(func) def clocked(*args, **kwargs): t0 = time.time() r 阅读全文
posted @ 2019-01-08 14:38 hailuo 阅读(421) 评论(0) 推荐(0) 编辑
摘要: import fileinput with fileinput.input(files=(path1,path2)) as f: for line in f: print(line) 阅读全文
posted @ 2019-01-08 10:59 hailuo 阅读(316) 评论(0) 推荐(0) 编辑
  2019年1月4日
摘要: 摘自《流畅的Python》 all 和 any 也是内置的归约函数。 all(iterable) 如果 iterable 的每个元素都是真值,返回 True;all([]) 返回 True。 any(iterable) 只要 iterable 中有元素是真值,就返回 True;any([]) 返回 阅读全文
posted @ 2019-01-04 14:16 hailuo 阅读(230) 评论(0) 推荐(0) 编辑
  2019年1月2日
摘要: import pandas as pd def coroutine(func): """装饰器:向前执行到第一个`yield`表达式,预激`func`""" @wraps(func) def primer(*args,**kwargs): gen = func(*args,**kwargs) next(gen) return gen primer.__name__ = fu... 阅读全文
posted @ 2019-01-02 17:27 hailuo 阅读(621) 评论(0) 推荐(0) 编辑
摘要: from collections import Iterable def flatten(items): for x in items: if isinstance(x, Iterable) and not isinstance(x, (str, bytes)): yield from flatte 阅读全文
posted @ 2019-01-02 16:47 hailuo 阅读(1087) 评论(0) 推荐(0) 编辑
摘要: #计数版 class countdown(object): def __init__(self,start): self.start = start def __iter__(self): return countdown_iter(self.start) class countdown_iter(object): def __init__(self, count): sel... 阅读全文
posted @ 2019-01-02 11:32 hailuo 阅读(423) 评论(0) 推荐(0) 编辑
  2018年12月31日
摘要: from collections import namedtuple Animal=namedtuple('Animal','name age type') perry=Animal(name='perry',age=1,type='cat') print(perry.type) print(perry[0]) print(perry._asdict()) 阅读全文
posted @ 2018-12-31 11:25 hailuo 阅读(213) 评论(0) 推荐(0) 编辑
摘要: 1 from collections import Counter 2 colours=( 3 ('Yasoob','Yellow',1), 4 ('Ali','Blue',2), 5 ('Arham','Green',3), 6 ('Ali','Black',4), 7 ('Yasoob','Red',5), 8 ('A... 阅读全文
posted @ 2018-12-31 11:13 hailuo 阅读(346) 评论(0) 推荐(0) 编辑
摘要: import collections import json tree=lambda:collections.defaultdict(tree) some_dict=tree() some_dict['colours']['favourite']='yellow' print(json.dumps(some_dict)) 阅读全文
posted @ 2018-12-31 10:45 hailuo 阅读(2557) 评论(0) 推荐(0) 编辑
  2018年12月10日
摘要: import pandas as pd df = pd.DataFrame({'AAA' : [1,2,1,3], 'BBB' : [1,1,2,2], 'CCC' : [2,1,3,1]}) source_cols = df.columns new_cols = [str(x) + "_cat" for x in source_cols] categories = {1 : 'Alpha', ... 阅读全文
posted @ 2018-12-10 15:13 hailuo 阅读(5198) 评论(0) 推荐(0) 编辑