2012-08-01 112 views
-2
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() 

輸出的Python跳過最後一個元素,而使用迭代循環的列表

['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'] 
+0

@Igor謝謝你的編輯,很難理解OP和他的代碼。 – 2012-08-01 10:48:41

+0

@lagor:感謝您的編輯。這是我第一次發佈代碼。對不起 – 2012-08-01 11:00:47

+0

如果您找到了解決方案,請從其中一個答案(我認爲您有答案)中將其標記爲這樣。我只提醒你這一點,因爲你提到你是該網站的新手。 – 2012-08-01 11:48:28

回答

1

這相當於你類,sans調試打印,並縮短變量名稱。

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 

,我也做了int_size_of_string一個屬性,因爲否則它是無法訪問的,一旦__init__已經完成(你不能從__init__返回一個值)。 strip方法從字符串的兩端刪除任意數量的空格和其他空白字符。

您的代碼無法工作的原因是因爲您正在修改您的列表,因爲您正在迭代它。您刪除了倒數第二個項目,所以最後一個項目取代了它的位置,然後當它移動到下一個項目時,沒有剩下的項目了。

+0

@ Lazyr:謝謝.. 我用兩個列表來解決你指定的問題,一個迭代和其他刪除項目(lst_temp_report_footer)並在迭代後將temp指定給原始。 謝謝你的快速回復:) – 2012-08-01 11:11:55

+0

編輯答案加主 – 2012-08-01 11:12:53

+0

@Jyohtish不,你沒有。 'lst_temp_report_footer'與'self.lst_report_footer_strings'是*相同的列表*。要製作副本,請使用'list(self.lst_report_footer_strings)'或'self.lst_report_footer_strings [:]'。 – 2012-08-01 11:14:42

1

我已經把它變成了一個更簡單的代碼,並試圖找出你真正想做的事情。有很多不必要的代碼,爲什麼你會用類來說明你的問題?

>>> x=['Manager', 'Accountant', 'Created By', 'fifth', '', ''] 
>>> result = [i for i in x if i.strip()] 
>>> result 
['Manager', 'Accountant', 'Created By', 'fifth'] 
>>> 
+0

@ Tim:謝謝.. 我只用一個類來指定類內部的代碼是我的實際程序的一部分,問題是,當我在終端上運行它時,因爲我需要..但是當我在我的代碼中使用此代碼。 py文件它不起作用。 非常感謝您的解決方案 – 2012-08-01 11:13:18

0

好吧,它不工作的原因是因爲你的循環遍歷列表中的每個項目。 當它找到一個空格時,它將刪除該項目。並顯示您當前的列表。所以第一次,它會顯示你的空間,因爲有2個。

但是,現在你的循環失敗了,因爲你在循環中修改了列表,所以沒有更多的項目可以循環使用,因爲你在6中的項目5中,然後你刪除了項目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() 

注意 - 有什麼奇怪在這個文件中,我希望它挑選出適合你的標籤。

+0

是的,其他答案更優雅,我試圖修復OP代碼...傻我.. :) – 2012-08-01 11:02:32

+0

@ Inbar:遺憾的是錯過了放置標籤..這是我上傳的第一個示例代碼..所以對齊對我來說很難.. :-P – 2012-08-01 11:31:08

相關問題