No subject

tiissa tiissa at nonfree.fr
Sat Apr 16 12:28:09 EDT 2005


Michael.Coll-Barth at VerizonWireless.com wrote:
> I have been going through the manuals and not having much luck with the
> following code.  This is basically an issue of giving 'split' multiple
> patterns to split a string.  If it had an ignore case switch, the problem
> would be solved.  Instead, I have to code the following, which works fine
> for a single byte string.  What can I do when I want to look for strings?
As far as I know there is no such switch.
If you can't convert your string in, say, lowercase, before splitting, 
you may have to use the re module:

In [1]: test = 'A big Fat CAt'

In [2]: import re

In [3]: A=re.compile('a', re.IGNORECASE)

In [4]: A.split(test)
Out[4]: ['', ' big F', 't C', 't']


>>>>test = 'A big Fat CAt'
>>>>A = test.split('A')
>>>>print A
> ['', ' big Fat C', 't']
>>>>a = []
>>>>for x in xrange(len(A)):
> ...     tmp = A[x].split('a')
> ...     for y in xrange(len(tmp)):
> ...         a.append(tmp[y])
> ...
>>>>a
> ['', ' big F', 't C', 't']
As a side note, reduce is great in such situations:

In [5]: reduce(list.__add__,[t.split('a') for t in test.split('A')],[])
Out[5]: ['', ' big F', 't C', 't']



More information about the Python-list mailing list