首页 > 编程语言 > 详细

python瞎练

时间:2019-08-18 16:23:04      阅读:77      评论:0      收藏:0      [点我收藏+]
  • 需求:有不规则列表 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)
  •  需求2:有列表 saiwa=[‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘每吨人工:‘, ‘‘, 90.0, ‘‘, ‘‘, ‘总人工‘, 1748.07, ‘‘, ‘‘],去除所有空格,这个需求不用递归,而采用遍历并判断元素是否为‘‘,从而决定是否删除时总会遇到删不干净的情况,甚为奇怪 
saiwa=[‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, 每吨人工:, ‘‘, 90.0, ‘‘, ‘‘, 总人工, 1748.07, ‘‘, ‘‘]
print(len(‘‘))
def delempty(mylist):
    if ‘‘ in mylist:
        mylist.pop(mylist.index(‘‘))
        delempty(mylist)
    return mylist
saiwa=delempty(saiwa)
print(finally:,saiwa)

 

python瞎练

原文:https://www.cnblogs.com/saintdingspage/p/11372405.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!