好吧,它不起作用的原因是因为循环遍历了列表中的每一项。
当它找到一个空格时,它会删除该项。并显示当前列表。所以第一次,它会显示空间,因为有两个。在
但是,现在你的循环失败了,因为你在循环中修改了列表,因此没有更多的项目可供它迭代,因为你在第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()
注意-这个文件中的标签有一些奇怪的地方,希望你能理解。在