Weird behavior in search in a list

Su Y suyuancn at gmail.com
Thu Mar 29 07:51:00 EDT 2007


hi all,
I can't understand how this code work, its behavior is really weird
for me...

I want find the first number in extend[] which is larger than num, so
I wrote:
def find(num):
    count=0
    for elem in extend:
        if elem<num:
            count+=1
    return count

I found that if extend[] is monotonous, like [1.1, 2.3, 3.2, 4.5,
5.6],
it works fine: find(4) returns 3, extend[3] is 4.5.
But, if extend[] is not monotonous, like [1.1, 2.3, 3.2, 4.5, 5.6,
4.6, 3.4, 2.1, 0.3],
find(4) returns 6, extend[6] is 3.4!

what's going on here? I really can't understand....




More information about the Python-list mailing list