Need help in understanding a python code

John Machin sjmachin at lexicon.net
Sun Nov 16 00:03:22 EST 2008


On Nov 16, 3:41 pm, "silverburgh.me... at gmail.com"
<silverburgh.me... at gmail.com> wrote:
> Hi,
>
> I am trying to understand the following line:
> # a is an integer array
>
> max([(sum(a[j:i]), (j,i))
>
> Can you please tell me what that means,
> I think sum(a[j:i] means find the some from a[j] to a[i]
> But what is the meaning of the part (j,i)?

0. "integer array" is a very loose term in Python. Fortunately the
answer to your question is not affected by that.
1. Sorry, the max... line is not syntactically correct; there are two
[s and only one ]; there are 4 (s and only 3 )s. Try copying the line
and pasting, not re-typing.
2. I'm not going to try to guess how to fix the bracket mismatches.
3. Note that you have left off a ) from your question about "sum" ...
it probably should be sum([j:i]).
4. That is the sum (not "some"!!) of a[j] to a[i-1] both inclusive.
It's a standard idiom in Python for the end of a range to be expressed
as the first unused element.
5. Even after fixing the bracket mismatches, it looks like you will
have an expression whose value is thrown away. Perhaps you might like
to give us a few lines of context before and after the line of
interest.



More information about the Python-list mailing list