随笔分类 - Python
摘要:import pickle from pathlib import Path file='d:/v/vbn' p=Path(file) if not p.parent.exists(): p.parent.mkdir(parents=True) with open(str(p),mode='wb')
阅读全文
摘要:from configparser import ConfigParser cfg=ConfigParser() cfg.read('c:/mysql.ini') print(cfg.sections()) print(cfg.has_section('client')) print(cfg.ite
阅读全文
摘要:open(file,mode='r',buffering=-1,encoding=None,errors=None,newline=None,closefd=True,opener=None) f=open("test") # file object # Windows <_io.TextIOWra
阅读全文
摘要:partial对参数进行复制后,会覆盖原有函数的默认值,因为,partial中已经赋值了,默认值就不再生效,如果partial中未定义,则默认值生效 partial定义 def partial(func,*args,**kwargs): def newfunc(*fargs,**fkwargs):
阅读全文
摘要:注册函数到一个名字,调用名字的时候,路由到注册函数,如果名字没有注册函数,路由到缺省函数 def cmd_dispatcher(): command={} def reg(name): # 注册函数装饰器 def _reg(fn): command[name]=fn return fn return
阅读全文
摘要:from functools import wraps import time,inspect,datetime def m_cache(duration): def _cache(fn): local_cache={} @wraps(fn) def wrapper(*args,**kwargs):
阅读全文
摘要:s1='abcdefg' s2='efgabcd' s3='zdxc' s4='mn' def findcom(str1,str2): if len(str1)>len(str2): str1,str2=str2,str1 commonstr=[] flag=False count=0 for su
阅读全文
摘要:dip=[x for x in range(1,10)]import math def jum(b): depth=math.ceil(math.log2(len(b)+1)) width=2**depth-1 print(depth,width) index=0 # 记录每次打印的索引 for x
阅读全文
摘要:source={'a':{'b':1,'c':2},'d':{'e':3,'f':{'g':4}}} target={} def flatmap(src,prefix=''): for k,v in src.items(): if isinstance(v,(dict,)): flatmap(v,p
阅读全文
摘要:import functools def logger(fn): # @functools.wraps(fn) def wrapper(*args,**kwargs): 'this is wrapper' print('before call fn') kin=fn(*args,**kwargs)
阅读全文
摘要:def copy_properties(src,dst): # 提供一个函数,被封装函数属性 copy 到包装函数属性,保持被封装函数属性不变 dst.__name__=src.__name__ dst.__doc__=src.__doc__ dst.__qualname__=src.__qualn
阅读全文
摘要:import random hip=list(range(10)) random.shuffle(hip) print(hip) def hum(b,reverse=False): hop=[] for x in b: for i,y in enumerate(hop): flag= x>y if
阅读全文
摘要:def jar(b): if not isinstance(b,int) or not b > 0: print('inaccurate') return None vv=len(str(b))+1 for i in range(b): for j in range(b-1,-1,-1): if j
阅读全文
摘要:''.join(reversed('abcd')) 切片 p[::-1] p[-1::-1] p[-1:-(len(p)+1:-1] from functools import reduce b = 'rty' print(reduce(lambda prev, curr: curr + prev,
阅读全文
摘要:错误示例: lst=list(range(10)) def remove_odd_index_elements(lst): b=0 for item in lst: if b&1: lst.remove(item) else: pass b+=1 remove_odd_index_elements(
阅读全文
摘要:dew=[] import random for _ in range(20): dew.append(random.randrange(1,21)) print(dew) count=[0]*20 for b in dew: count[b-1]+=1 print(count) for p in
阅读全文
摘要:ego=[] era=[] for b in range(1,10): ego.append(b) if not b%3: era.append(ego) ego=[] # ego.clear() print(era) for b in range(len(era[0])): for p in ra
阅读全文
摘要:ego=[] era=[] for b in range(1,10): ego.append(b) if not b%3: era.append(ego) ego=[] # ego.clear() print(era) ego.clear() ego变成空列表,但是ego变量还是指向源地址 ego=
阅读全文
摘要:m=int(input('line: ').strip().lstrip('0')) while True: k=int(input('number: ').strip()) if k == m or k == 1: print(1) exit(10) elif k > m: print('wron
阅读全文
摘要:dip = [] for _ in range(3): dip.append(int(input('{}: '.format(_)))) if dip[0]>dip[1]: if dip[0]>dip[2]: if dip[1]>dip[2]: dim=[dip[2],dip[1],dip[0]]
阅读全文

浙公网安备 33010602011771号