标签云

微信群

扫码加入我们

WeChat QR Code

I'm looking for a string.contains or string.indexof method in Python.I want to do:if not somestring.contains("blah"): continue


this question is discussed at meta

2019年05月24日29分16秒

Under the hood, Python will use __contains__(self, item), __iter__(self), and __getitem__(self, key) in that order to determine whether an item lies in a given contains. Implement at least one of those methods to make in available to your custom type.

2019年05月24日29分16秒

Just make sure that somestring won't be None. Otherwise you get a TypeError: argument of type 'NoneType' is not iterable

2019年05月24日29分16秒

FWIW, this is the idiomatic way to accomplish said goal.

2019年05月24日29分16秒

For strings, does the Python in operator use the Rabin-Carp algorithm?

2019年05月24日29分16秒

This is inconsistent and ugly in code like ".so." in filename or filename.endswith(".blah").

2019年05月24日29分16秒

+1 for highlighting the gotchas involved in substring searches. the obvious solution is if ' is ' in s: which will return False as is (probably) expected.

2019年05月24日29分16秒

aaronasterling Obvious it may be, but not entirely correct. What if you have punctuation or it's at the start or end? What about capitalisation? Better would be a case insensitive regex search for \bis\b (word boundaries).

2019年05月24日29分16秒

JamieBull Once again, you must consider if you want to include punctuation as a delimiter for a word. Splitting would have largely the same effect as the naive solution of checking for ' is ', notably, it won't catch This is, a comma' or 'It is.'.

2019年05月24日29分16秒

JamieBull: I highly doubt any real input split with s.split(string.punctuation + string.whitespace) would split even once; split isn't like the strip/rstrip/lstrip family of functions, it only splits when it sees all of the delimiter characters, contiguously, in that exact order. If you want to split on character classes, you're back to regular expressions (at which point, searching for r'\bis\b' without splitting is the simpler, faster way to go).

2019年05月24日29分16秒

'is' not in (w.lower() for w in s.translate(string.maketrans(' ' * len(string.punctuation + string.whitespace), string.punctuation + string.whitespace)).split() - ok, point taken. This is now ridiculous...

1970年01月01日00分09秒

Why should one avoid str.index and str.find? How else would you suggest someone find the index of a substring instead of just whether it exists or not? (or did you mean avoid using them in place of contains - so don't use s.find(ss) != -1 instead of ss in s?)

2019年05月24日29分16秒

Precisely so, although the intent behind the use of those methods may be better addressed by elegant use of the re module. I have not yet found a use for str.index or str.find myself in any code I have written yet.

2019年05月24日29分16秒

Could the list iterable be switched around to look for any of the list in a single string? Ex: ["bar", "foo", "foobar"] in "foof"?

2019年05月24日29分16秒

CaffeinatedCoder, no, this requires nested iteration. Best done by joining the list with pipes "|".join(["bar","foo", "foobar"]) and compiling a regex out of it, then matching on "foof"

2019年05月24日29分16秒

I figured out early that it could also be done with a generator, which allowed me to avoid regex. Thanks for an alternative though!

2019年05月24日29分16秒

any([x in "foof" for x in ["bar", "foo", "foobar"]])

2019年05月24日29分16秒

IzaakWeiss Your one liner works, but it's not very readable, and it does nested iteration. I would advice against doing this

2019年05月24日29分16秒

That's because there is a bajillion ways of creating a Product from atomic variables.You can stuff them in a tuple, a list (which are forms of Cartesian Products and come with an implied order), or they can be named properties of a class (no a priori order) or dictionary values, or they can be files in a directory, or whatever.Whenever you can uniquely identify (iter or getitem) something in a 'container' or 'context', you can see that 'container' as a sort of vector and define binary ops on it.en.wikipedia.org/wiki/…

2019年05月24日29分16秒

counting a string is costly when you just want to check if it's there...

2019年05月24日29分16秒

methods that exist in the original post from 2010 so I ended up editing them out, with consensus from community (see meta post meta.stackoverflow.com/questions/385063/…)

2019年05月24日29分16秒

no. My point is "why answering the exact same thing as others did 9 years ago" ?

2019年05月24日29分16秒

because I'm moderating the site... I've asked the question on meta meta.stackoverflow.com/questions/385063/…

2019年05月24日29分16秒

then If you have the authority to remove it then remove it, else do what you must and move on. IMO this answer adds value, which is reflected by up-votes from users.

2019年05月24日29分16秒