444,050 Members | 1,020 Online
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

# Loop and the list

 P: n/a 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 " 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 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. -- Krzysztof Szynter :'''. :. : * Dygi GG 1027078 :...' ..... : : : ..... . . . . . ..... http://newbie.friko.pl : : :.... : : : :.... :: :: :.. : :.... dygimail(at)poczta(dot)fm :...' :.... : ': :.... : : :..' : :.... Jul 18 '05 #1
12 Replies

 P: n/a Krzysztof Szynter wrote: 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

 P: n/a Krzysztof Szynter wrote in news:Xn******************************@127.0.0.1: [...] 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[i]): -- rzed Jul 18 '05 #3

 P: n/a Krzysztof Szynter wrote: Hi all Have got a big problem. This is my code: [snipped] If I understand you correctly, you just need to find maximum. If it's so, there is better way: built-in max function: max([1, 2, 3]) 3 regards, anton. Jul 18 '05 #4

 P: n/a On 14 May 2004 13:46:46 GMT, Krzysztof Szynter wrote: Hi allHave got a big problem. This is my code: def maxvalue(TY): #gets an index of the max value in the TY list max = 0 indexMax = -1 for i in range(0,len(TY)): if (max

 P: n/a 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[i] = TY[i]/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). -- Krzysztof Szynter :'''. :. : * Dygi GG 1027078 :...' ..... : : : ..... . . . . . ..... http://newbie.friko.pl : : :.... : : : :.... :: :: :.. : :.... dygimail(at)poczta(dot)fm :...' :.... : ': :.... : : :..' : :.... Jul 18 '05 #6

 P: n/a anton muhin wrote in news:2gk3eeF3gha1U1@uni- berlin.de: max([1, 2, 3]) 3 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. -- Krzysztof Szynter :'''. :. : * Dygi GG 1027078 :...' ..... : : : ..... . . . . . ..... http://newbie.friko.pl : : :.... : : : :.... :: :: :.. : :.... dygimail(at)poczta(dot)fm :...' :.... : ': :.... : : :..' : :.... Jul 18 '05 #7

 P: n/a Tjarko de Jong wrote in news:r8********************************@4ax.com: indexMax = -1 Don't get it. Why '-1'? -- Krzysztof Szynter :'''. :. : * Dygi GG 1027078 :...' ..... : : : ..... . . . . . ..... http://newbie.friko.pl : : :.... : : : :.... :: :: :.. : :.... dygimail(at)poczta(dot)fm :...' :.... : ': :.... : : :..' : :.... Jul 18 '05 #8

 P: n/a Krzysztof Szynter wrote: anton muhin wrote in news:2gk3eeF3gha1U1@uni- berlin.de: >> max([1, 2, 3]) 3 It works. Sometimes. It just works. max([1,2,3]) 3 Until you rebind it: max = 123 max([1,2,3]) Traceback (most recent call last): File "", line 1, in ? TypeError: 'int' object is not callable Peter Jul 18 '05 #9

 P: n/a Peter Otten <__*******@web.de> wrote in news:c8*************@news.t- online.com: 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? -- Krzysztof Szynter :'''. :. : * Dygi GG 1027078 :...' ..... : : : ..... . . . . . ..... http://newbie.friko.pl : : :.... : : : :.... :: :: :.. : :.... dygimail(at)poczta(dot)fm :...' :.... : ': :.... : : :..' : :.... Jul 18 '05 #10

 P: n/a Krzysztof Szynter wrote: def normowanie(TY,max): "normalize the function" for i in range(0,len(TY)-1): TY[i] = TY[i]/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 Jul 18 '05 #11

 P: n/a Krzysztof Szynter wrote: Peter Otten <__*******@web.de> wrote in news:c8*************@news.t- online.com: 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? There is no conceptual difference between functions and objects. You can both bind them ad libitum: alpha = max max = 123 max 123 alpha alpha(1,2,3) 3 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: class Int(int): .... def __call__(self): .... print "I'm a callable integer. Weird, n'est-ce pas?" .... i = Int() i 0 i() I'm a callable integer. Weird, n'est-ce pas? Peter Jul 18 '05 #12

 P: n/a Peter Otten <__*******@web.de> wrote in news:c8*************@news.t-online.com: There is no conceptual difference between functions and objects. You can both bind them ad libitum: Understand. Thank a lot. I'm a callable integer. Weird, n'est-ce pas? La vie est mysterieuse. ps Thanks for patience. -- Krzysztof Szynter :'''. :. : * Dygi GG 1027078 :...' ..... : : : ..... . . . . . ..... http://newbie.friko.pl : : :.... : : : :.... :: :: :.. : :.... dygimail(at)poczta(dot)fm :...' :.... : ': :.... : : :..' : :.... Jul 18 '05 #13

### This discussion thread is closed

Replies have been disabled for this discussion.