我试图理解是否有必要将列表的内容附加到另一个列表中。
我已经通过循环函数创建了第一个列表,它将从文件中获取特定的行并将它们保存在列表中。然后使用第二个列表来保存这些行,并在另一个文件上启动一个新的循环。
我的想法是在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