38

I am trying to fumble through python, and learn the best way to do things. I have a string where I am doing a compare with another string to see if there is a match:

if paid[j].find(d)>=0:
    #BLAH BLAH

If d were an list, what is the most efficient way to see if the string contained in paid[j] has a match to any value in d?

2
  • Can you explain what you mean by a match to any value in 'd'? What do you consider to be a "match"? Do you require the strings to be identical? Commented May 6, 2010 at 20:04
  • 1
    Thanks for your reply. Yes I meant lists. Sorry, still learning. I meant to see if there was a pattern match between paid[j] (a sentence) and any of the words in the list 'd'. Commented May 6, 2010 at 20:15

4 Answers 4

59

If you only want to know if any item of d is contained in paid[j], as you literally say:

if any(x in paid[j] for x in d): ...

If you also want to know which items of d are contained in paid[j]:

contained = [x for x in d if x in paid[j]]

contained will be an empty list if no items of d are contained in paid[j].

There are other solutions yet if what you want is yet another alternative, e.g., get the first item of d contained in paid[j] (and None if no item is so contained):

firstone = next((x for x in d if x in paid[j]), None)

BTW, since in a comment you mention sentences and words, maybe you don't necessarily want a string check (which is what all of my examples are doing), because they can't consider word boundaries -- e.g., each example will say that 'cat' is in 'obfuscate' (because, 'obfuscate' contains 'cat' as a substring). To allow checks on word boundaries, rather than simple substring checks, you might productively use regular expressions... but I suggest you open a separate question on that, if that's what you require -- all of the code snippets in this answer, depending on your exact requirements, will work equally well if you change the predicate x in paid[j] into some more sophisticated predicate such as somere.search(paid[j]) for an appropriate RE object somere. (Python 2.6 or better -- slight differences in 2.5 and earlier).

If your intention is something else again, such as getting one or all of the indices in d of the items satisfying your constrain, there are easy solutions for those different problems, too... but, if what you actually require is so far away from what you said, I'd better stop guessing and hope you clarify;-).

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

5 Comments

This is an excellent explanation, thank you. My goal is I parsed all this html code looking for links. Once I have a list of them, I go thru that list and compare it with the master list 'd'. The reason I want to compare string matches is I may find a link in the parsed html that goes to domain.com, but it wouldn't match anything in 'd', although I would like it to match against www.domain.com. From your explanation, this would accomplish what I want, right? if any(x in paid[j] for x in d): ...
@Halik, yes, as long as you also want to find (e.g.) notreallywww.domain.com and www.domain.com.fooledya.org. If you don't want such spurious matches, you need a more sophisticated approach than plain string-matching. But the if any(... part is still correct, just with a different predicate (better open a separate question for that part, though).
Thanks! Just opened a new question regarding this topic.
Why I receive "NameError: name 'any' is not defined" when I'm trying to if any(...) ?
The firstone = next((generator), default) is an amazingly elegant code snippet. Thanks a lot for that eye-opener.
12

I assume you mean list and not array? There is such a thing as an array in Python, but more often than not you want a list instead of an array.

The way to check if a list contains a value is to use in:

if paid[j] in d:
    # ...

Comments

11

In Python you may use the in operator. You can do stuff like this:

>>> "c" in "abc"
True

Taking this further, you can check for complex structures, like tuples:

>>> (2, 4, 8) in ((1, 2, 3), (2, 4, 8))
True

1 Comment

Worked perfectly for me, no need for semi-complicated loops. +1
3
for word in d:
    if d in paid[j]:
         do_something()

will try all the words in the list d and check if they can be found in the string paid[j].

This is not very efficient since paid[j] has to be scanned again for each word in d. You could also use two sets, one composed of the words in the sentence, one of your list, and then look at the intersection of the sets.

sentence = "words don't come easy"
d = ["come", "together", "easy", "does", "it"]

s1 = set(sentence.split())
s2 = set(d)

print (s1.intersection(s2))

Output:

{'come', 'easy'}

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.