Mining strings from a HTML document.

Magnus Lycka lycka at carmen.se
Thu Jan 26 09:25:49 EST 2006


Derick van Niekerk wrote:
> Could you/anyone explain the 4 lines of code to me though? A crash
> course in Python shorthand? What does it mean when you use two sets of
> brackets as in : beg = [1,0][text.startswith(s1)] ?

It's not as strange as it looks. [1,0] is a list. If you put []
after a list, it's for indexing, right? (Unless there's one or
two ':' somehere, in which case it's slicing.)

text.startswith(s1) evaluates to True or False, which is equivalent
to 1 or 0 in a numerical context. [1,0][0] is 1, and [1,0][1] is
0, so you could say that it's a somewhat contrieved way of writing
"beg = int(not text.startswith(s1))" or "beg = 1 - text.startswith(s1)"



More information about the Python-list mailing list