Weird behavior in search in a list



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....

.



Relevant Pages

  • Re: Weird behavior in search in a list
    ... I can't understand how this code work, ... I want find the first number in extend[] which is larger than num, ... reason it works in your sorted list scenario is because elem will be> num, ...
    (comp.lang.python)
  • Re: Weird behavior in search in a list
    ... def find: ... findloops through the list, and every time it finds a value less ... than num it increments count. ... return min([x for x in extend if x> num]) ...
    (comp.lang.python)
  • Re: Weird behavior in search in a list
    ... def find: ... findloops through the list, and every time it finds a value less than num it increments count. ... return min([x for x in extend if x> num]) ...
    (comp.lang.python)
  • Re: Weird behavior in search in a list
    ... I can't understand how this code work, ... I want find the first number in extend[] which is larger than num, ... and I am using Python 2.5 on WinXP. ...
    (comp.lang.python)
  • Re: Weird behavior in search in a list
    ... On 3/29/07, Michael Bentley wrote: ... > I want find the first number in extend[] which is larger than num, ... Endless the world's turn, endless the sun's Spinning, Endless the quest; ...
    (comp.lang.python)