10.19日笔试总结
---恢复内容开始---
Test('1','2').result('c')的输出结果为1|2|c
请实现Test类 (继承object类)的初始化及result函数,仅在result函数中输出
class Test(object): def __init__(self,a,b): self.arglist=[] self.arglist.extend([a,b]) def result(self,c): res='' for curarg in self.arglist: res+=curarg res+='|' res+=c return res print Test('1','2').result('c')
实现 u'中国'与'中国'的互转
u'中国'.encode('utf-8')
unicode('中国','utf-8')
实现lista , dict b,str c, class Test的深拷贝
实现list a的深拷贝:
def deepcopy_list(something): lookdic={} def helper(thing): if isinstance(thing,list): if id(thing) in lookdic: return lookdic[id(thing)] else: tmp=[] lookdic[id(thing)]=tmp for elem in thing: tmp.append(helper(elem)) return tmp return thing return helper(something) # tmp=[1, [2, [3, 4], 5]] # tmp1=deepcopy1(tmp) # tmp1[1].append(6) #print tmp,tmp1
实现 dict b的深拷贝:
def deepcopy_dic(something): lookdic={} def helper(thing): if isinstance(thing,dict): if id(thing) in lookdic: return lookdic[id(thing)] else: tmp={} lookdic[id(thing)]=tmp for elem in thing: tmp[elem]=helper(thing[elem]) return tmp return thing return helper(something) tmp={'a':{'e':4},'b':2,'c':3} #tmp1=deepcopy_dic(tmp) tmp1=tmp tmp1['a']['f']=5 print tmp,tmp1
执行func()后,抛出KeyError异常并捕捉,捕捉不做处理,并且无论是否有其他异常都打印当前时间戳
def func(a): tmp={'b':1} print tmp[a] try: func('a') except KeyError: print 'keyerror' pass finally: print int(time.time())
请根据命令行参数打开文件,逐行读入、输出,关闭文件(最多两行代码)
---恢复内容结束---