a = ['AKDYYDSSGYHFDY', 'AKDDSSGYYFYFDY', 'AKDAGDYYYYGMDV']
match = ['DS', 'DV', 'DY']
counter = 0
for i in a:
for j in match:
if j in i:
print(i, j)
counter = counter 1
continue
print(counter)
Results are
AKDYYDSSGYHFDY DS
AKDYYDSSGYHFDY DY
AKDDSSGYYFYFDY DS
AKDDSSGYYFYFDY DY
AKDAGDYYYYGMDV DV
AKDAGDYYYYGMDV DY
6
I was expecting that it would identify the first pattern DS in the first string in list a, then move to next element. However, it proceed to identify DY as well. What am I doing incorrectly? Any help is appreciated.
Thanks
CodePudding user response:
Try to replace continue
with break
like this
a = ['AKDYYDSSGYHFDY', 'AKDDSSGYYFYFDY', 'AKDAGDYYYYGMDV']
match = ['DS', 'DV', 'DY']
counter = 0
for i in a:
for j in match:
if j in i:
print(i, j)
counter = counter 1
break
print(counter)
Output:
AKDYYDSSGYHFDY DS
AKDDSSGYYFYFDY DS
AKDAGDYYYYGMDV DV
3
сontinue
actually means that you go to the next iteration of for
loop. Since you have continue inside j
loop it doesn't influence on i
loop and simply iterates more on j
.
break
instead stops iterations on j
loop and let's i
loop to proceed on the next iteration