2016-03-05 39 views
-2

從未有過這樣的問題,不知道如何解決,甚至把它稱爲...的Python for循環只有每秒項目

types = [105000, 105001, 105002, 105003, 105004, 105005, 105006, 105007] 
for type in types: 
    print type 

只打印

105000 
105002 
105004 
105006 

但我不知道爲什麼。

使用函數來生成從輸入像陣列

self.arrSplit(105000,105001,105002,105003,105004,105005,105006,105007) 

功能:

def arrSplit(self, arr): 
     itms = [] 
     for it in arr.split(','): 
      tt = it.split('-') 
      if (len(tt) >= 2 and int(tt[0]) < int(tt[1])): 
       for i in range(int(tt[0]), int(tt[1])+1): 
        itms.append(i) 
      else: 
       itms.append(int(tt[0])) 
     return itms 

完成此代碼看起來是這樣的(其最小)

types = self.arrSplit(105000,105001,105002,105003,105004,105005,105006,105007) 
print types 
for type in types: 
print type 

版畫:

[105000, 105001, 105002, 105003, 105004, 105005, 105006, 105007] 
105000 
105002 
105004 
105006 
+0

這是不可能的。你確定這是產生該輸出的代碼嗎? – styvane

+0

對我來說工作得很好......儘管如此,你的縮進已被打破,但我想這是你如何將它放在SO上的一種人造物。SO –

+0

請給出意見(是你提到的'1,2,3,4'? ),預期的返回值和'arrSplit'的實際返回值。 –

回答

0

對於for循環,你應該這樣做:

types = [105000, 105001, 105002, 105003, 105004, 105005, 105006, 105007] 
for numbers in types: 
    print numbers 

這爲我工作,但我無法解釋爲什麼它只是脣上印。