如何在Python中连接两个列表?
例:
listone = [1, 2, 3]
listtwo = [4, 5, 6]
Run Code Online (Sandbox Code Playgroud)
预期结果:
>>> joinedlist
[1, 2, 3, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud) 我试图理解是否有必要获取列表的内容并将其附加到另一个列表.
我通过循环函数创建了第一个列表,它将从文件中获取特定行并将它们保存在列表中.
然后使用第二个列表来保存这些行,并在另一个文件上开始一个新的循环.
我的想法是在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 …Run Code Online (Sandbox Code Playgroud)