之前有一个文档,我对其中的数值要进行自动化添加,但是里面的数值是无序的,每当遇到一个值为空的元素都要向上找到最后一个数值进行自增,并且要算上中间有多少个空的值就循环几次经过我处理了过后,文档已经能够自动判断添加过后的新dict.keys,但我始终无法完成自增这一块的逻辑和方法相关代码defdo_str_2():dict1=[]dict2=[]count=0withopen(sys.path[0]+'/success.txt','r')asf:forfinf.readlines():if'='inf:f=f.replace('\n','').replace(',','').split('=')f[0]=f[0].strip()f[1]=f[1].strip()#print(f)result_dict[f[0]]=f[1]else:f=f.replace('\n','').split(',')iflen(f)>1:f[0]=f[0].strip()f[1]=f[1].strip()#print(f)result_dict[f[0]]=f[1]forvalueinresult_dict.values():dict1.append(value)print(dict1)最终处理完成后得到的结果是这样一个列表,这个列表就是字典的值['00','01','','','100000','','','','','','','','','','','','','','','','','','10100','10200','200000','','','','','','20100','20200','1','','','','100','101','102','103','111','112','114','115','120','121','123','124','199','10000','10002','10003','10100','10102']我最终要得到的结果为下面的列表一样,但是里面的值是根据上一个最后不为空的值进行添加的,比如说['00','01','','']那么现在里面有两个空元素就应该找到01这个值作为基数来循环两次,结果就为['00','01','02','03'],求大神解答['00','01','02','03','100000','100001','100002','100003','100004','100005','100006','100007','100008','100009','100010','100011','100012','100013','100014','100015','100016','100017','10100','10200','200000','200001','200002','200003','200004','200005','20100','20200','1','2','3','4','100','101','102','103','111','112','114','115','120','121','123','124','199','10000','10002','10003','10100','10102']
2 回答
慕哥9229398
TA贡献1877条经验 获得超6个赞
参考deffoo(ls):assertlsandls[0]!=''ls2=list(ls)foriinrange(len(ls2)):ifls2[i]=='':ls2[i]=str(int(ls2[i-1])+1)returnls2
慕妹3242003
TA贡献1824条经验 获得超6个赞
楼上的少了一个补零的操作,在这里补上deffoo(lst):fori,_inenumerate(lst):ifnot_:lst[i]=str(int(lst[i-1])+1).zfill(len(lst[i-1]))returnlstlst=['00','01','','','100000','','','','','','','','','','','','','','','','','','10100','10200','200000','','','','','','20100','20200','1','','','','100','101','102','103','111','112','114','115','120','121','123','124','199','10000','10002','10003','10100','10102']printfoo(lst)
添加回答
举报
0/150
提交
取消