3

I have the following code

import re

pattern = ['A-minor Type I AGC', 'A-minor Type I AGC', 'A-minor Type I AGC', 'A-minor Type I AUA', 'A-minor Type I AUA', 'A-minor Type II AGC', 'A-minor Type II AGC']

n = len(pattern)
print pattern
pattern_str = ', '.join(pattern)
print pattern_str
for x in range(0, n):
    if re.search(r'\bType I\b', pattern_str):
        print "Hello Type I"
    elif re.search(r'\bType II\b', pattern_str):
        print "Hello Type II"
    else:
        print "An error has occured"

The desired output should be:

Hello Type I
Hello Type I
Hello Type I
Hello Type I
Hello Type I
Hello Type II
Hello Type II

But I'm not getting the desired output. My current output is:

Hello Type I
Hello Type I
Hello Type I
Hello Type I
Hello Type I
Hello Type I
Hello Type I

Can someone point out the problem? I suspect it has to be something to do with the list to str conversion. I have managed to solve the problem using the following code:

for x in pattern:
    if re.search(r'\bType I\b', x):
        print "Hello Type I"
    elif re.search(r'\bType II\b', x):
        print "Hello Type II"
    else:
        print "An error has occured"   

But I would like to know why my first code didn't work and how can I make it work. Any help is appreciated

2
  • Well - you initialized the pattern_str only once in the list and that's why you are getting the result. You could do - what you've done (more correct) or following change for x in range(0,n): pattern_str = pattern[x] Commented May 21, 2015 at 3:14
  • @gabhijit Thank you very much for your answer. Your advice to change pattern_str = pattern[x] made the first code to work. Thanks again :) Commented May 21, 2015 at 3:26

3 Answers 3

2

You're joining the whole list into a single string, then testing that a whole bunch of times. Instead, what you want is to test each string in the list like

for pattern_str in pattern:
    if re.search(r'\bType I\b', pattern_str):
        print "Hello Type I"
    elif re.search(r'\bType II\b', pattern_str):
        print "Hello Type II"
    else:
        print "An error has occured"

so you are searching each pattern, one at a time

Sign up to request clarification or add additional context in comments.

Comments

1

What you want: search through each string in your list.

What your code does

re.search(r'\bType I\b', pattern_str)

It searches through pattern_str in every iteration of the loop. What is pattern_str:

pattern_str = ', '.join(pattern)

Thus, in every iteration, it searches through the same string, which is the concatanation of the whole list, which always matches Type I in A-minor Type I AGC.

Changing in to search through each x in pattern does the trick

Comments

0

Your pattern is the same for each iteration. You need to loop through your patterns in your for-loop.

Use for p in pattern

This will mean p is 'A-minor Type I AGC' on the first iteration, 'A-minor Type I AGC' on the second iteration, etc.

import re

pattern = ['A-minor Type I AGC', 'A-minor Type I AGC', 'A-minor Type I AGC', 'A-minor Type I AUA', 'A-minor Type I AUA', 'A-minor Type II AGC', 'A-minor Type II AGC']

for p in pattern:
    if re.search(r'\bType I\b', p):
        print "Hello Type I"
    elif re.search(r'\bType II\b', p):
        print "Hello Type II"
    else:
        print "An error has occured"

Output:

Hello Type I
Hello Type I
Hello Type I
Hello Type I
Hello Type I
Hello Type II
Hello Type II

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.