[Tutor] printing answers
Christopher Spears
cspears2002 at yahoo.com
Thu Feb 5 16:18:07 EST 2004
I'm writing a function that takes a list of strings
and finds any identical strings. Case should not
matter. For example, in ['one','One', 'two', 'three',
'ThrEE'], one and One and three and ThrEE should be
recognized as pairs.
I wrote the following code:
def findMatch(list_of_strings):
import re
answers = []
for i in range(len(list_of_strings)):
pattern = re.compile(list_of_strings[i], re.I)
for i in range(len(list_of_strings)):
match = re.match(pattern,
list_of_strings[i])
if match != None:
answers = answers +
[list_of_strings[i]]
print answers
My problem is printing out the correct answers. When
I ran a test, I got the following result:
>>> findMatch(['one', 'two', 'One'])
['one', 'One', 'two', 'one', 'One']
What am I doing wrong? I think my logic is correct.
-Chris
More information about the Tutor
mailing list