我想了解是否有意義將列表的內容添加到另一個列表。Python:獲取列表的內容並將其附加到另一個列表
我有通過循環功能創建的第一個列表,它將從文件中獲取特定行並將它們保存在列表中。
然後第二個列表用於保存這些行,並在另一個文件上開始一個新的循環。
我的想法是一旦完成for循環,將其轉儲到第二個列表中,然後開始一個新的循環,將第一個列表的內容再次轉儲到第二個列表中,但將其附加到第二個列表中,列表將是我的循環中創建的所有較小列表文件的總和。只有在滿足某些條件的情況下,該列表才能被附加。
看起來類似於這樣:
# This is done for each log in my directory, i have a loop running
for logs in mydir:
for line in mylog:
#...if the conditions are met
list1.append(line)
for item in list1:
if "string" in item: #if somewhere in the list1 i have a match for a string
list2.append(list1) # append every line in list1 to list2
del list1 [:] # delete the content of the list1
break
else:
del list1 [:] # delete the list content and start all over
這樣做是有道理還是我應該去不同的路線?
我需要一些效率高,不會佔用太多週期,因爲日誌列表很長,每個文本文件都很大;所以我認爲名單會符合目的。
聽起來很光滑!我會看看它是否可以替換我已經使用itertools的代碼! – user1006198