2013-12-18 62 views
-3

我正在嘗試使用python編寫腳本。我需要避免重複相同的任務,並且想要寫入一個。在for循環中調用多個列表python

catlist1 = ['s0.05-k5-a1.0' , 's0.05-k5-a3.0' , 's0.05-k5-a7.0' , 's0.05-k5-a10.0' ] 
catlist2 = ['s0.05-k7-a1.0' , 's0.05-k7-a3.0' , 's0.05-k7-a7.0' , 's0.05-k7-a10.0'] 

catlist3 = ['s0.07-k5-a1.0' , 's0.07-k5-a3.0' , 's0.07-k5-a7.0' , 's0.07-k5-a10.0' ] 
catlist4 = ['s0.07-k7-a1.0' , 's0.07-k7-a3.0' , 's0.07-k7-a7.0' , 's0.07-k7-a10.0'] 

catlist = [catlist1 ,catlist2 ,catlist3 ,catlist4 ] 

我需要調用for循環中的每個列表。我打電話給這個單一循環內的所有列表。我可以通過多次寫入catlist1和catlist2來重複執行此操作。 我該如何解決這個問題。謝謝

for parName in catlist:   

    category = '/home/x/Desktop/rouge/ROUGE/Experiments/' 

    for root, subFolders, files in os.walk(category + parName): 
     #i = 0 
     (head, filename) = os.path.split(root) 
     print filename 
     #some function that is will re 

回答

2

你想要做的就是調用,扁平化一個嵌套列表。你可以像下面這樣做

for parName in (item for items in catlist for item in items): 
+0

我一直在尋找是在for循環迭代,直到一個列表(catlist1)結束每次迭代和其他名單上的下一個列表迭代(catlist2) 。 –

+0

@MeronGebremariam是。這確實是...... :) – thefourtheye

5
for parName in itertools.chain.from_iterable(catlist):