python瞎练

  • 需求:有不规则列表 singlelist3 = [ '总计', '每吨人工:', '总人工', 1748.07, '金额'],如果当前元素为字符串且该元素的下一个相邻位置仍为字符串,那么请在该元素后面插入数字0,如同 singlelist3 = [ '总计',0.00, '每吨人工:',0.00, '总人工', 1748.07,‘金额’,0.00]
def expandstr(mylist):
if isinstance(mylist[-1],str):
mylist.append(0.00)
indexlist = []
i=0
while i<len(mylist)-1:
myval = mylist[i]
if isinstance(mylist[i+1], str) and isinstance(myval, str):
indexlist.append(i)
else:
print("hi")
i+=1
indexlist=list(map(lambda x:x+1,indexlist))
mylist.insert(indexlist[0],0.00)
i=1
while i<len(indexlist):
mylist.insert(indexlist[i]+1,0.00)
i+=1
print("expand: ",indexlist)
print("expand: ",mylist)

猜你喜欢

转载自www.cnblogs.com/saintdingspage/p/11372405.html