Python在使用for循环迭代列表时跳过中的最后一个元素
输出Python在使用for循环迭代列表时跳过中的最后一个元素,python,Python,输出 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
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
for lst_report_footer_item in self.lst_report_footer_strings:
print lst_temp_report_footer
print lst_report_footer_item
if lst_report_footer_item in ('', ' '):
print "Inside if : Item ==" + lst_report_footer_item
lst_temp_report_footer.remove(lst_report_footer_item)
print "list after remove == " + str(lst_temp_report_footer)
else:
print "Inside else : length = ", str(len(lst_report_footer_item))
int_size_of_string += len(lst_report_footer_item)
if __name__ == '__main__':
ins_class = Sample()
我需要的是……
['Manager', 'Accountant', 'Created By', 'fifth', '', '']
Manager
Inside else : length = 7
['Manager', 'Accountant', 'Created By', 'fifth', '', '']
Accountant
Inside else : length = 10
['Manager', 'Accountant', 'Created By', 'fifth', '', '']
Created By
Inside else : length = 10
['Manager', 'Accountant', 'Created By', 'fifth', '', '']
fifth
Inside else : length = 5
['Manager', 'Accountant', 'Created By', 'fifth', '', '']
Inside if : Item ==
list after remove == ['Manager', 'Accountant', 'Created By', 'fifth', '']
这相当于您的类,具有缩短的变量名
list after remove == ['Manager', 'Accountant', 'Created By', 'fifth']
我还将int\u size\u of_string
作为一个属性,因为在\uuu init\uuu
完成后,它就无法访问(您不能从\uuu init\uu
返回值)。strip
方法从字符串的两端删除任意数量的空格和其他空白字符
代码不起作用的原因是,在迭代列表时修改了列表。您删除了倒数第二个项目,因此最后一个项目取而代之,然后当它移动到下一个项目时,就没有剩下任何项目了。我已将其转换为一段简单得多的代码,并试图找出您真正想要做的事情。有很多不必要的代码,为什么要用类来说明问题
class Sample:
def __init__(self):
self.footers = ['Manager', 'Accountant', 'Created By', 'fifth', '', '']
self.footers = [x for x in self.footers if x.strip() != ""]
self.int_size_of_string = sum(len(x) for x in self.footers)
if __name__ == '__main__':
myclass = Sample()
print myclass.footers
print myclass.int_size_of_string
好的,它不起作用的原因是因为循环遍历了列表中的每个项目。 当它找到一个空间时,它将删除该项。并显示您当前的列表。因此,第一次,它将显示空间,因为有两个 但是,现在您的循环失败了,因为您在循环中修改了列表,因此没有更多的项可供其循环,因为您在6项中的第5项,然后删除了第5项,现在列表仅包含5项,但循环查找第6项。既然没有,它就退出了。让名单保持原样 基本上,您应该做以下几点:
>>> x=['Manager', 'Accountant', 'Created By', 'fifth', '', '']
>>> result = [i for i in x if i.strip()]
>>> result
['Manager', 'Accountant', 'Created By', 'fifth']
>>>
注意-此文件中的选项卡有些奇怪,我希望能为您解决。@Igor感谢您的编辑,很难理解OP及其代码。@lagor:感谢您的编辑。这是我第一次发布代码。对不起,如果你已经找到了一个解决方案,从这些答案中的一个(我想你已经找到了),请将其标记为这样。我之所以提醒你这一点,是因为你提到你是该网站的新用户。是的,其他答案要优雅得多,我正试图修复操作码。。。我真傻……)@Inbar:很抱歉,未放置标签。。这是我上传的第一个示例代码。。因此,对我来说,对齐很难-P@Lazyr:谢谢。。我使用了两个列表来解决您指定的问题,一个用于迭代,另一个用于删除项(lst_temp_report_footer),并在迭代后将temp分配给原始项。感谢您的快速响应:)编辑了要添加的答案main@Jyohtish不,你没有
lst\u temp\u report\u footer
与self.lst\u report\u footer\u strings
的列表相同。要复制,请使用list(self.lst\u report\u footer\u strings)
或self.lst\u report\u footer\u strings[:]
@lazyr:谢谢你,lazyr。。我是python新手,不知道这个问题…-)非常感谢@Tim:谢谢。。我使用一个类只是为了指定类中的代码是我实际程序的一部分,问题是当我在终端上运行它时,它可以根据需要工作。。但是,当我在.py文件中使用此代码时,它不起作用。非常感谢您的解决方案
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()