partial / wildcard string match in 'in' and 'list.index()'
jcarlson at uci.edu
Fri Jun 4 09:25:07 CEST 2004
> These would work, but I was wondering if there was some compact
> way to get 'in' and lists.index() to do wildcard matching as opposed
> to exact matching.
Short answer: no.
There is a regular expression library, aptly called 're', for non-exact
The standard string methods only do exact searching likely because there
are algorithms that make it fast (linear in the length of the searched
string), that are also quite small.
There exist very simple looking regular expressions that force searches
to take exponential (in the length of the pattern) time to search, and
are generally fairly large.
An example of an exponentially slow regular expression is contained in
the following example...
>>> import re
>>> import time
>>> def test(n):
... a = 100*'0'
... t = time.time()
... re.search('(0+)'*n, a)
... return time.time()-t
>>> for i in xrange(18, 23):
... print i, test(i)
Trust me, you don't want that kind of slow down when you are searching
in strings with "if i in 'somestring':".
More information about the Python-list