2016-05-11 93 views
0

我在下面提供的代碼一次打印輸出一行。但是,我想重寫代碼以一次打印所有內容。一次全部打印

def filters(): 
      for LogLine in Log: 
       flag = True 
       for key,ConfLine in Conf.items(): 
        for patterns in ConfLine: 
         print patterns 
         if re.match((DateString + patterns), LogLine): 
          flag = False 
          break 

        if(flag == False): 
         break 

       if(flag): 

       print LogLine 

感謝

回答

0

有一件事我會做。我會初始化一個空字典或空列表,然後將所有項目追加到空字典或空列表中。最後一起打印輸出。

def filters(): 
    mypatterns=[] 
    for LogLine in Log: 
     flag = True 
     for key,ConfLine in Conf.items(): 
      for patterns in ConfLine: 
       print patterns 
       mypatterns.append(patterns) 
       if re.match((DateString + patterns), LogLine): 
        flag = False 
        break 
      if(flag == False): 
       break 
     if(flag): 
      print LogLine 

print mypatterns 
2

這裏的一般技術:

lines = [] 
for ... 
    lines.append(<whatever you were going to print>) 
print '\n'.join(lines)