猿问

请各位大佬指点!Python如何对列表空字符赋值,每个字符串都以上一个检索到存在的值作为基数自增

之前有一个文档,我对其中的数值要进行自动化添加,但是里面的数值是无序的,每当遇到一个值为空的元素都要向上找到最后一个数值进行自增,并且要算上中间有多少个空的值就循环几次
经过我处理了过后,文档已经能够自动判断添加过后的新dict.keys,但我始终无法完成自增这一块的逻辑和方法
相关代码
defdo_str_2():
dict1=[]
dict2=[]
count=0
withopen(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']
侃侃尔雅
浏览 370回答 2
2回答

慕哥9229398

参考deffoo(ls):assertlsandls[0]!=''ls2=list(ls)foriinrange(len(ls2)):ifls2[i]=='':ls2[i]=str(int(ls2[i-1])+1)returnls2

慕妹3242003

楼上的少了一个补零的操作,在这里补上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)
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答