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

Magnus Lyckå magnus@thinkware.se
Fri May 2 06:20:02 2003


At 00:24 2003-05-02 -0400, 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

This only works in Python 2.3 and above. From 2.3,
"a in b" will basically be the same thing as
"b.find(a) != -1", but for now, you need to do...

     if line.find(target) == -1:

...to achieve what you want. Read the library reference chapter 2.
(It's a while since I said that, but it's still as relevant.)

>what's a char left operand?

I suppose "needs a char as its left operand" would have been
better English. Or perhaps even "needs a one character long
string as its left operand".

A "char" is a string with lenght 1. I guess this is not
completely obvious for people who haven't programmed in
languages such as C, where a string is just a sequence of
chars. I doubt the error message will change though, since
the behaviour is already changed...

A "left operand" is the object or expression on the left
hand side of an operator, in this case the "in" operator.

For "3 + 5", 3 is the left operator, 5 is the right operator
and + is the binary operator. It's called binary operator
since it wants two operands. A unary operator only wants
one operand. "-" is both a binary operator (subtraction) and
a unary operator (negation). Python has no ternary operator
(yet) but many other languages have one (but only one).


--
Magnus Lycka (It's really Lyckå), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The shortest path from thought to working program