python瞎练
- 需求:有不規則列表 singlelist3 = [ '總計', '每噸人工:', '總人工', 1748.07, '金額'],如果當前元素為字符串且該元素的下一個相鄰位置仍為字符串,那么請在該元素后面插入數字0,如同 singlelist3 = [ '總計',0.00, '每噸人工:',0.00, '總人工', 1748.07,‘金額’,0.00]
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)
- ?需求2:有列表?saiwa=['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '每噸人工:', '', 90.0, '', '', '總人工', 1748.07, '', ''],去除所有空格,這個需求不用遞歸,而采用遍歷并判斷元素是否為'',從而決定是否刪除時總會遇到刪不干凈的情況,甚為奇怪?
?
轉載于:https://www.cnblogs.com/saintdingspage/p/11372405.html
總結
- 上一篇: Eaplayer漂亮好用的wordpre
- 下一篇: 电脑知识:分享实用的电脑维护小常识