python for 循环中最后一个不要_Python在使用for循环迭代列表时跳过中的最后一个元素...

好吧,它不起作用的原因是因为循环遍历了列表中的每一项。

当它找到一个空格时,它会删除该项。并显示当前列表。所以第一次,它会显示空间,因为有两个。在

但是,现在你的循环失败了,因为你在循环中修改了列表,因此没有更多的项目可供它迭代,因为你在第5项中的第6项,然后你删除了第5项,现在列表只包含5项,但循环查找第6项。既然没有,就退出。让名单保持原样。在

基本上,你应该做的是:class Sample:

def __init__(self):

self.lst_report_footer_strings = \

['Manager', 'Accountant', 'Created By', 'fifth', '', '']

int_size_of_string = 0

lst_temp_report_footer = self.lst_report_footer_strings

temp_remove_list = []

for lst_report_footer_item in xrange(len(self.lst_report_footer_strings)):

print lst_temp_report_footer

print self.lst_report_footer_strings[lst_report_footer_item]

if lst_temp_report_footer[lst_report_footer_item] in ['',' ']:

print "Inside if : Item =="+self.lst_report_footer_strings[lst_report_footer_item]

temp_remove_list.append(lst_report_footer_item)

else:

print "Inside else : length = ",str(len(lst_temp_report_footer[lst_report_footer_item]))

int_size_of_string += len(lst_temp_report_footer[lst_report_footer_item])

for item in reversed(temp_remove_list):

del lst_temp_report_footer[item]

print "final list : == ",lst_temp_report_footer

if __name__ == '__main__':

ins_class = Sample()

注意-这个文件中的标签有一些奇怪的地方,希望你能理解。在

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值