伪代码:
A = [1,2,3,4,5,6,7,8,9]
B = [4,5,6,7,1,2,6,7,8]
count = 0
for i in range(len(A)):
for j in range(len(B)):
if A[i:i+3] == B[j:j+3]: #check 3 consecutive numbers if are equal
count += 1
print x[i:i+3]
print count
问题:如何在A[4,5,6] == B[4,5,6]
时实施,然后跳至A[6,7,8]==B[6,7,8]
,而不是A[5,6,7]==B[5,6,7]
答案 0 :(得分:1)
您可以使用标志变量:
A = [1,2,3,4,5,6,7,8,9]
B = [4,5,6,7,1,2,6,7,8]
count = 0
skip = False #this is a flag variable
for i in range(len(A)):
for j in range(len(B)):
if skip:
skip = False
continue
if A[i:i+3] == B[j:j+3]: #check 3 consecutive numbers if are equal
count += 1
print x[i:i+3]
skip = True
print count