以下是比较两个列表并返回在相同索引中匹配项的示例代码:
list1 = [1,2,3,4,5]
list2 = [1,2,4,6,8]
for index, item in enumerate(list1):
if item == list2[index]:
print(item)
输出结果为:
1
2
在这个例子中,我们首先使用enumerate()函数来遍历列表1中的所有项,并同时获取它们的索引。在每个迭代中,我们检查列表1的当前项是否与列表2中相同索引处的项匹配。如果匹配,则我们打印出该项。