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.... 8 1231
On 3ÔÂ29ÈÕ, ÏÂÎç7ʱ51·Ö, "Su Y" <suyua...@gmail.comwrote:
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.
On 29 Mar 2007 04:51:00 -0700, Su Y <su******@gmail.comwrote:
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,
--
----
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.
On 29 Mar 2007 04:51:00 -0700, Su Y <su******@gmail.comwrote:
<snip>
I want find the first number in extend[] which is larger than num, so
<snip>
On 3/29/07, Amit Khemka <kh********@gmail.comwrote:
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.
"Su Y" <su******@gmail.comwrote in message
news:11*********************@e65g2000hsc.googlegro ups.com...
On 3ÔÂ29ÈÕ, ÏÂÎç7ʱ51·Ö, "Su Y" <suyua...@gmail.comwrote:
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
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<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
On 3/29/07, Michael Bentley <mi*****@jedimindworks.comwrote:
>
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
<snip>
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.
On 3ÔÂ29ÈÕ, ÏÂÎç8ʱ22·Ö, Michael Bentley <mich...@jedimindworks.comwrote:
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<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!
"Su Y" <s...nc*@gmail.comwrote:
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: python newbie |
last post by:
hey,
okay, I'm trying to figure out why my books: Quick Python, Python in a
Nutshell, Python Cookbook and Learning Python don't say anything about the
weird behavior of a list when you have one as...
|
by: atv |
last post by:
at least to me it is. I can't figure out for the life what it is i'm
doing wrong here.
i have a function called assign_coordinate. usually, i pass a malloced pointer
to it, then individual...
|
by: mrstevegross |
last post by:
I ran into a weird behavior with lexical scope in Python. I'm hoping
someone on this forum can explain it to me.
Here's the situation: I have an Outer class. In the Outer class, I
define a...
|
by: TamaThps |
last post by:
Hi, I'm using visual studio 2008 and normally when I get an error it shows what line it is on and which file etc. The error I'm getting I don't know how to solve or even what the problem is. This...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |