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?
If you only want to know if any item of
dis contained inpaid[j], as you literally say:If you also want to know which items of
dare contained inpaid[j]:containedwill be an empty list if no items ofdare contained inpaid[j].There are other solutions yet if what you want is yet another alternative, e.g., get the first item of
dcontained inpaid[j](andNoneif no item is so contained):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 predicatex in paid[j]into some more sophisticated predicate such assomere.search(paid[j])for an appropriate RE objectsomere.(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
dof 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;-).