Weird behavior in search in a list

S

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

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

Amit Khemka

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

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,


--
 
J

Josh

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

Amit Khemka

On 29 Mar 2007 04:51:00 -0700 said:
I want find the first number in extend[] which is larger than num, so
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?
--
 
M

Michael Bentley

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

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
 
A

Amit Khemka

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

Su Y

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

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

Hendrik van Rooyen

Su Y said:
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....

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top