1

If I have an list of strings:

matches = [ 'string1', 'anotherstring', 'astringystring' ]

And I have another string that I want to test:

teststring = 'thestring1'

And I want to test each string, and if any match, do something. I have:

match = 0
for matchstring in matches:
  if matchstring in teststring:
    match = 1
if !match:
  continue

This is in a loop, so we just go around again if we don't get a match (I can reverse this logic of course and do something if it matches), but the code looks clumsy and not pythonic, if easy to follow.

I am thinking there is a better way to do this, but I don't grok python as well as I would like. Is there a better approach?

Note the "duplicate" is the opposite question (though the same answer approach is the same).

8
  • you need full or partial match? Commented Feb 18, 2016 at 9:19
  • if any(match in teststring for match in matches) Commented Feb 18, 2016 at 9:20
  • @viakondratiuk the 'in' does a substring match Commented Feb 18, 2016 at 9:22
  • FWIW using a compiled regex would probably be faster than any and allow you to return any matched substring Commented Feb 18, 2016 at 9:36
  • 1
    @PadraicCunningham Thanks, yes, I am not concerned about speed here, and in this instance I don't need to know what the match was, only that it matched. So if either of these aren't true in future, regex. Commented Feb 18, 2016 at 9:40

3 Answers 3

6

You could use any here

Code:

if any(matchstring in teststring for matchstring in matches):
    print "Matched"

Notes:

  • any exits as soon it see's a match.
  • As per as the loop what is happening is for matchstring in matches here each string from the matches is iterated.
  • And here matchstring in teststring we are checking if the iterated string is in the defined check string.
  • The any will exit as soon as it see's a True[match] in the expression.
Sign up to request clarification or add additional context in comments.

10 Comments

Yes, I wasn't aware of any
Though I cannot follow the x in y for x in z - how is that working?
in operator for strings checks for substring occurence, for o in seq iterates over sequence. These are two different functionalities with same keyword used.
@Rogalski Yes, I understand the two uses of in, I just don't understand how the matchstring created in the for can be tested at the beginning
@Paul added see if it makes sense.
|
2

If you want to know what the first match was you can use next:

match = next((match for match in matches if match in teststring), None)

You have to pass None as the second parameter if you don't want it to raise an exception when nothing matches. It will use the value as the default, so match will be None if nothing is found.

Comments

0

How about you try this:

len([ x for x in b if ((a in x) or (x in a)) ]) > 0

I've updated the answer to check the substring both ways. You can pick and choose or modify as you see fit but I think the basics should be pretty clear.

3 Comments

This would evaluate every string in matches even if you matched the first string and create a list to throw away.
True that it goes a step further in also counting the matches and maybe not required for this step but then again it might be.
sum with a generator expression would also count the matches and avoid the need to store a list, this is not a use case for a list comp and len.

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.