Loop and the list

K

Krzysztof Szynter

Hi all

Have got a big problem. This is my code:

-----BEGIN
#stale programowe
G = 6.67E-11
Mz = 6E+24
Rz = 6.37E+6
TX = range(10,780,2)
TY = []

def skoknatezenia(ilpkt):
"Oblicza skok"
dr = ((2*Rz)-Rz)/ilpkt
return dr

def natezeniep(TX,TY,dr):
"Liczy natezenie w przedziale <Rz,2*Rz>"
cnt = 0
for i in range(0,2*Rz,dr):
TY.append((G*Mz)/((Rz+cnt)*(Rz+cnt)))
cnt = cnt + dr
return TY

def maxvalue(TY): #gets an index of the max value in the TY list
max = 0
for i in range(0,len(TY)):
if (max<TY):
max = i
print TY,max
continue
return max

def normowanie(TY): #normalize the TY list items
for i in range(0,len(TY)-1):
TY = TY/float(TY[0]) #for example '0', should be 'max'
return TY

Koniec = False
while Koniec <> True:
try:
Koniec2 = False
while Koniec2 <> True:
ilpkt = raw_input("Podaj ilosc punktow do wykreslenia 200):\t")
try:
int(ilpkt)
ilpkt = int(ilpkt)
Koniec2 = True
except:
print "Ilosc punktow musi byc liczba calkowita!\t"
continue
Koniec = True
except:
print "Nieoczekiwany blad. Sprobuj jeszcze raz."

dr = skoknatezenia(ilpkt)
TY = natezeniep(TX,TY,dr)
print TY #first main point
max = maxvalue(TY)
print "max =",max
TY = normowanie(TY) #normalize the function
print TY
-----END

I don't understand why function maxvalue returns "9". When i start a
program and check TY list in 'first main point', the max value has an index
0'. Then putting the list into the function maxvalue, gives me the index
'9'. ;--/

Where is the mistake?

ps
Apologise for the Polish text's, in the code. I'll answer for any question.
 
?

=?ISO-8859-2?Q?Holger_T=FCrk?=

Krzysztof said:
def maxvalue(TY): #gets an index of the max value in the TY list
max = 0
for i in range(0,len(TY)):
if (max<TY):
max = i
print TY,max
continue
return max


max is an index, right? So your test should be

if TY[max] < TY:
[....]

In your code, you compare the index to a value.

Greetings,

Holger
 
R

rzed

[...]
I don't understand why function maxvalue returns "9". When i
start a program and check TY list in 'first main point', the max
value has an index 0'. Then putting the list into the function
maxvalue, gives me the index '9'. ;--/

Where is the mistake?

ps
Apologise for the Polish text's, in the code. I'll answer for
any question.

In maxvalue, you are comparing an index to a value, which seems
unlikely to be what you want. Did you mean:
if (TY[max] < TY):
 
K

Krzysztof Szynter

According to your question and advice:

Yes i mean indexing test in the if statement. Just corrected this simple
bug. But it still doesn't work. This is a piece of results, the code gives:

[1]

TY = natezeniep(TX,TY,dr)
print TY #first main point

gives a list:

[9.8627544243153125, 9.764861685914024, 9.6684191984269319,
9.5733984559832201, 9.479771649668697, 9.3875116471769786,
9.2965919731504503, 9.2069867901844269, 9.118670880469038,
9.0316196280445151, 8.9458090016465412, 8.8612155381193709,
8.7778163263753228, 8.695588991880193, 8.6145116816449576,
8.5345630497049747, 8.4557222430686831, 8.3779688881185095,
8.3012830774474473, 8.2256453571154164, ...]

[2]

max = maxvalue(TY)
print "max =",max

gives (now) a correct index '0', but...

[3]

TY = normowanie(TY) #normalize the function
print TY

gives a non correct () list:

[1.0, 9.764861685914024, 9.6684191984269319, 9.5733984559832201,
9.479771649668697, 9.3875116471769786, 9.2965919731504503,
9.2069867901844269, 9.118670880469038, 9.0316196280445151,
8.9458090016465412, 8.8612155381193709, 8.7778163263753228,
8.695588991880193, 8.6145116816449576, 8.5345630497049747,
8.4557222430686831, 8.3779688881185095, 8.3012830774474473,
8.2256453571154164, 8.151036714310175, ...]

[conclusion]

So the only good item is '0' indexed. For better look there is the code
responsible for the bug:

def normowanie(TY,max):
"normalize the function"
for i in range(0,len(TY)-1):
TY = TY/float(TY[max])
return TY

The function call has arguments: max = '0' and the list TY from [1] at the
top of the post.

ps
Normalizing, i mean to divide (float) all the items in TY list by the max
value in the list. So the result list should contain of items between '0'
and '1' (float of course, not rounded to int).
 
K

Krzysztof Szynter


It works. Sometimes.

Don't know why, but executing the code, crashes the shell. I mean
the shell missunderstood the commands, so i cannot even check max()
working. The shell saw max() call like an int() call ;--<

After a while...

Aarggghhhh. Can somebeody tell me, why without starting this ^&%$ code,
i have normally working shell, with max() function call, but after
executing the code, my shell starts to treat ONLY max() function as
int(), and the code crashes ale the time with:

'int' object is not collable

But without touching the code i can call max() in shell any time i want.
 
K

Krzysztof Szynter

It just works.

Ok. I got it. But why there is no difference between max and max()? I
thought both are independent. I treat 'max' like a variable and 'max()'
like a function, so they can live independently. Even i gave 'max' a value
(integer value for example).

Am i wrong? Or i didn't get it yet?
 
P

Peter Otten

Krzysztof said:
def normowanie(TY,max):
"normalize the function"
for i in range(0,len(TY)-1):
TY = TY/float(TY[max])
return TY


While you iterate over TY, for i==max TY[max] suddenly changes to 1, and now
all further items are divided by 1, i. e. remain unchanged.

Remove the second argument altogether, remove your maxvalue() function and
then put the following line at the beginning of your script:

from __future__ import division # indicate that we want
# 1/2 == 0.5 instead of 0

Now the revised normalization function (untested):

def normowanie(TY):
m = max(TY)
return [v/m for v in TY]

This creates a normalized copy of the original list. You can call it:

normalizedTY = normowanie(TY)

If you want to change TY in place, i. e. you need not keep the original
list, here's that variant:

def normowanie(TY):
m = max(TY)
TY[:] = [v/m for v in TY]

As this follows the example of the mutating methods like list.append() and
list.sort() and doesn't return the list, call it like so:

normowanie(TY)

All items in TY are now in the range 0 <= v <= 1 (assuming there were no
negative values in the first place), but the original values are lost.

Peter
 
P

Peter Otten

Krzysztof said:
Ok. I got it. But why there is no difference between max and max()? I
thought both are independent. I treat 'max' like a variable and 'max()'
like a function, so they can live independently. Even i gave 'max' a value
(integer value for example).

Am i wrong? Or i didn't get it yet?

There is no conceptual difference between functions and objects. You can
both bind them ad libitum:

That a function remembers its original name is only a convenience for the
user. Functions are just objects that implement a () operator aka "callable
objects". Here's a (simplified) callable integer:
.... def __call__(self):
.... print "I'm a callable integer. Weird, n'est-ce pas?"
....
Peter
 

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,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top