Weird behavior in search in a list

Su Y suyuancn at gmail.com
Thu Mar 29 07:55:06 EDT 2007


On 3月29日, 下午7时51分, "Su Y" <suyua... at gmail.com> wrote:
> 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, soI 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....

and I am using Python 2.5 on WinXP.




More information about the Python-list mailing list