JavaScript
x
14
14
1
a = ['AKDYYDSSGYHFDY', 'AKDDSSGYYFYFDY', 'AKDAGDYYYYGMDV']
2
3
match = ['DS', 'DV', 'DY']
4
5
counter = 0
6
for i in a:
7
for j in match:
8
if j in i:
9
print(i, j)
10
counter = counter+1
11
continue
12
13
print(counter)
14
Results are
JavaScript
1
10
10
1
AKDYYDSSGYHFDY DS
2
AKDYYDSSGYHFDY DY
3
AKDDSSGYYFYFDY DS
4
AKDDSSGYYFYFDY DY
5
AKDAGDYYYYGMDV DV
6
AKDAGDYYYYGMDV DY
7
8
6
9
10
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
Advertisement
Answer
Try to replace continue
with break
like this
JavaScript
1
14
14
1
a = ['AKDYYDSSGYHFDY', 'AKDDSSGYYFYFDY', 'AKDAGDYYYYGMDV']
2
3
match = ['DS', 'DV', 'DY']
4
5
counter = 0
6
for i in a:
7
for j in match:
8
if j in i:
9
print(i, j)
10
counter = counter+1
11
break
12
13
print(counter)
14
Output:
JavaScript
1
5
1
AKDYYDSSGYHFDY DS
2
AKDDSSGYYFYFDY DS
3
AKDAGDYYYYGMDV DV
4
3
5
с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