[Tutor] searching for a substring in a list of strings.

Sean 'Shaleh' Perry shalehperry@attbi.com
Fri May 2 02:17:02 2003


On Thursday 01 May 2003 21:24, Kirk Bailey wrote:
> target=mylist+':"|/'           # all list aliases start as(listname):"|/
> for line in aliases:           # each 'line' is a string in a list
>      if target not in line:     #ie, do NOT write the line with our
>          f1.write(line+'\n')    # write the non targeted line
> f2.close()                     # now close the file.
>
> produces this error:
> Traceback (innermost last):
>    File "/www/www.tinylist.org/cgi-bin/TLlistkill2.py", line 185, in ?
>      if target not in line:	#ie, do NOT write the line with our
> TypeError: string member test needs char left operand
>
> what's a char left operand?
>

'if target not in line' treats line like a list of characters.  So, the input 
to the 'in' operator should be a character.

> I am searching for a substring occuring in a list of strings. IF that
> subscring exists in a particular string, which is one of N strings in a
> list, the routine is to NOT write out that string to the file. The list
> is a file read in using readlines, creating a list. Each line becomes a
> string in the list. I want to search all the lines ion the list for the
> occurance of the string.
>
> Aliases always start with a name, a ':', then the beginning of the
> definition of the alias. This is always
> (listname):"|/
>
> Meaqnwhile, I continue searching the on line documentation and my short
> stack of 2 manuals.

>>> s = "foo:bar"
>>> s.find('bar')
4
>>> s.find('baz')
-1