我正在尝试通过使用另一个列表进行for循环来检查列表中字符串的数量。我在list1中搜索双精度索引,但我的问题是,一旦在列表中找到第一个字符串,当找到第二个实例时索引不会上升。我希望在这种情况下0,1,2,3,但实际上它是0,1,1,3。
list1 = ['ABBA', 'CBBA', 'CBBA','DBBA']
list2 = ['ABBA','CBBA','DBBA']
for line in list1:
for marker in list2:
if marker in line:
print("Marker found", list1.index(line))
答案 0 :(得分:1)
您可以enumerate
列表,以便知道哪一行是哪个索引:
lines = ['ABBA', 'CBBA', 'CBBA','DBBA']
markers = ['ABBA','CBBA','DBBA']
for index, line in enumerate(lines):
for marker in markers:
if marker in line:
print("Marker found", index)
输出:
Marker found 0
Marker found 1
Marker found 2
Marker found 3