[Tutor] Counting matching elements in sequences
Karl Pflästerer
sigurd at 12move.de
Wed Dec 10 15:10:34 EST 2003
On 10 Dec 2003, Magnus Lycka <- magnus at thinkware.se wrote:
>> > def alleq(seq):
>> > for item in seq[1:]:
>> > if item != seq[0]:
>> > return False
>> > return True
>> I came up with nearly the same one.
>> def alleq(seq):
>> k = seq[0]
>> for i in seq:
>> if i != k:
>> return 0
>> return 1
> The reason I didn't write it like that was to
> avoid the IndexError you get in "k = seq[0]"
> if seq is empty.
Right. And I wanted to avoid the lookup in the sequence every cycle in
the loop. But if we enclose it i a `If seq: ...' then everything is
fine.
Karl
--
Please do *not* send copies of replies to me.
I read the list
More information about the Tutor
mailing list