Re: Weird behavior in search in a list

2007-03-30 Thread Hendrik van Rooyen
"Su Y" <[EMAIL PROTECTED]> wrote:

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

Hint: extend[0]  is1.1
Hint: extend[7] is 2.1
Hint: 2.1 is less than 4

You have to stop counting and come out of the loop when you find the 
first one - what your function is doing is counting the elements less than
num, not finding the first one that is.

hth - Hendrik


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Weird behavior in search in a list

2007-03-29 Thread Su Y
On 3月29日, 下午8时22分, Michael Bentley <[EMAIL PROTECTED]> wrote:
> On Mar 29, 2007, at 6:51 AM, Su Y wrote:
>
>
>
>
>
> > 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 > 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
>
> find() loops through the list, and every time it finds a value less
> than num it increments count.  So in your second example the values
> 1.1, 2.3, 3.2,  3.4, 2.1, and 0.3 are all less than 4, which means
> count will be 6.
>
> As you learned, a sorted list behaves as you expect.  So one approach
> is to sort your list first.  But another perhaps better approach is
> to do something like:
>
> def find(num):
> # check to make sure there *is* a value greater than num
> if max(extend) > num:
> # then return the smallest value that is greater than num
> return min([x for x in extend if x > num])
> else:
> return None
>
> Hope this helps,
> Michael

oh yes, it's the "break" I forgot... Thank you, it helps me a lot!

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Weird behavior in search in a list

2007-03-29 Thread Amit Khemka
On 3/29/07, Michael Bentley <[EMAIL PROTECTED]> wrote:
>
> On Mar 29, 2007, at 6:51 AM, Su Y wrote:
> >
> > I want find the first number in extend[] which is larger than num, so


> def find(num):
> # check to make sure there *is* a value greater than num
> if max(extend) > num:
> # then return the smallest value that is greater than num
> return min([x for x in extend if x > num])
> else:
> return None

I think OP wants the first value in the list greater than 'num' not
the smallest greater value in the list.

cheers,
-- 

Amit Khemka -- onyomo.com
Home Page: www.cse.iitd.ernet.in/~csd00377
Endless the world's turn, endless the sun's Spinning, Endless the quest;
I turn again, back to my own beginning, And here, find rest.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Weird behavior in search in a list

2007-03-29 Thread Michael Bentley

On Mar 29, 2007, at 6:51 AM, Su Y wrote:
>
> 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 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

find() loops through the list, and every time it finds a value less  
than num it increments count.  So in your second example the values  
1.1, 2.3, 3.2,  3.4, 2.1, and 0.3 are all less than 4, which means  
count will be 6.

As you learned, a sorted list behaves as you expect.  So one approach  
is to sort your list first.  But another perhaps better approach is  
to do something like:

def find(num):
# check to make sure there *is* a value greater than num
if max(extend) > num:
# then return the smallest value that is greater than num
return min([x for x in extend if x > num])
else:
return None

Hope this helps,
Michael
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Weird behavior in search in a list

2007-03-29 Thread Josh

"Su Y" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
On 3ÔÂ29ÈÕ, ÏÂÎç7ʱ51·Ö, "Su Y" <[EMAIL PROTECTED]> 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 count+=1
  else:
 break
> return count
>
you need to break out of the loop when you first encounter num>elem. The 
reason it works in your sorted list scenario is because elem will be > num, 
always, after some point. It won't be in your unsorted list.

I've added the else: break in your code above
j


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Weird behavior in search in a list

2007-03-29 Thread Amit Khemka
> On 29 Mar 2007 04:51:00 -0700, Su Y <[EMAIL PROTECTED]> wrote:

> > I want find the first number in extend[] which is larger than num, so


> On 3/29/07, Amit Khemka <[EMAIL PROTECTED]> wrote:
> Btw a concise way could be:
> def getfirstbigger(num):
> for i,x in enumerate(extend):
> if x>num:return i
> return len(extend)

I forgot to add that you can as well 'return' the the item (along with
the index), but in case all the items in the list are smaller than the
argument, what return value do you require?
-- 

Amit Khemka -- onyomo.com
Home Page: www.cse.iitd.ernet.in/~csd00377
Endless the world's turn, endless the sun's Spinning, Endless the quest;
I turn again, back to my own beginning, And here, find rest.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Weird behavior in search in a list

2007-03-29 Thread Amit Khemka
On 29 Mar 2007 04:51:00 -0700, Su Y <[EMAIL PROTECTED]> 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, so
> I wrote:
> def find(num):
> count=0
> for elem in extend:
> if elem 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

Actually your function "find" returns the number of elements in list
extend, which are smaller than the argument. Perhaps you wanted to
'break' when once the condition is met.

 def find(num):
 count=0
 for elem in extend:
 if elem>num: break
 else: count+=1
 return count

Btw a concise way could be:
def getfirstbigger(num):
for i,x in enumerate(extend):
if x>num:return i
return len(extend)

HTH,


-- 

Amit Khemka -- onyomo.com
Home Page: www.cse.iitd.ernet.in/~csd00377
Endless the world's turn, endless the sun's Spinning, Endless the quest;
I turn again, back to my own beginning, And here, find rest.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Weird behavior in search in a list

2007-03-29 Thread Su Y
On 3月29日, 下午7时51分, "Su Y" <[EMAIL PROTECTED]> 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 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.

-- 
http://mail.python.org/mailman/listinfo/python-list

Weird behavior in search in a list

2007-03-29 Thread Su Y
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-- 
http://mail.python.org/mailman/listinfo/python-list