![]() |
Basic question
I've been studying python for 2 weeks now and got stucked in the
following problem: for j in range(10): print j if(True): j=j+2 print 'interno',j What happens is that "j=j+2" inside IF does not change the loop counter ("j") as it would in C or Java, for example. Am I missing something? []'s Cesar |
Re: Basic question
> "j=j+2" inside IF does not change the loop > counter ("j") You might be not truly catching the idea of Python `for` statements sequence nature. It seems that <http://docs.python.org/ref/for.html> will make things quite clear. > The suite may assign to the variable(s) in the target list; this > does not affect the next item assigned to it. In C you do not specify all the values the "looping" variable will be assigned to, unlike (in the simplest case) you do in Python. -- Happy Hacking. Dmitry "Sphinx" Dzhus http://sphinx.net.ru |
Re: Basic question
Cesar G. Miguel wrote:
> for j in range(10): > print j > if(True): > j=j+2 > print 'interno',j > > What happens is that "j=j+2" inside IF does not change the loop > counter ("j") as it would in C or Java, for example. > Am I missing something? If you want that kind of behaviour then use a `while` construct: j = 0 while j < 5: print j if True: j = j + 3 print '-- ', j If you use a for loop, for each pass through the foor loop Python assigns next item in sequence to the `j` variable. HTH, Karlo. |
Re: Basic question
On May 12, 5:18 pm, "Cesar G. Miguel" <cesar.go...@gmail.com> wrote:
> I've been studying python for 2 weeks now and got stucked in the > following problem: > > for j in range(10): > print j > if(True): > j=j+2 > print 'interno',j > > What happens is that "j=j+2" inside IF does not change the loop > counter ("j") as it would in C or Java, for example. > > Am I missing something? Yes you are :) "for j in range(10):..." means: 1. Build a list [0,1,2,3,4,5,6,7,8,9] 2. For element in this list (0, then 1, then 2,...), set j to that value then execute the code inside the loop body To simulate "for(<initialisation>; <condition>; <increment>) <body>" you have to use while in Python: <initialisation> while <condition>: <body> <increment> Of course in most case it would not be the "pythonic" way of doing it :) -- Arnaud |
Re: Basic question
Cesar G. Miguel wrote:
> I've been studying python for 2 weeks now and got stucked in the > following problem: > > for j in range(10): > print j > if(True): > j=j+2 > print 'interno',j > > What happens is that "j=j+2" inside IF does not change the loop > counter ("j") as it would in C or Java, for example. > > Am I missing something? > > []'s > Cesar > > Nope. The loop counter will be assigned successively through the list of integers produced by range(10). Inside the loop, if you change j, then from that point on for that pass through the body, j will have that value. But such an action will not change the fact that next pass through the loop, j will be assigned the next value in the list. |
Re: Basic question
On May 12, 12:18 pm, "Cesar G. Miguel" <cesar.go...@gmail.com> wrote:
> I've been studying python for 2 weeks now and got stucked in the > following problem: > > for j in range(10): > print j > if(True): > j=j+2 > print 'interno',j > > What happens is that "j=j+2" inside IF does not change the loop > counter ("j") as it would in C or Java, for example. > > Am I missing something? > > []'s > Cesar What is your real intent here? This is how I understand it after reading your post: you want to create a loop that steps by an increment of 2. If that's the case, then: >>> for j in range(0,10,2): .... print j .... 0 2 4 6 8 would be a simple result. Cheers, -Basilisk96 |
Re: Basic question
On 12 May 2007 09:18:06 -0700, "Cesar G. Miguel" <cesar.gomes@gmail.com>
declaimed the following in comp.lang.python: > > Am I missing something? > Python is not C or Java... for x in range(10): builds a list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] so for x in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]: Internally, Python keeps track of where it is in the list. The "x" you see is what Python found at the "current position" in the list. Changin "x" makes no changes to the list -- nor to the internal position used by the "for". -- Wulfraed Dennis Lee Bieber KD6MOG wlfraed@ix.netcom.com wulfraed@bestiaria.com HTTP://wlfraed.home.netcom.com/ (Bestiaria Support Staff: web-asst@bestiaria.com) HTTP://www.bestiaria.com/ |
Re: Basic question
On May 12, 2:45 pm, Basilisk96 <basilis...@gmail.com> wrote:
> On May 12, 12:18 pm, "Cesar G. Miguel" <cesar.go...@gmail.com> wrote: > > > > > I've been studying python for 2 weeks now and got stucked in the > > following problem: > > > for j in range(10): > > print j > > if(True): > > j=j+2 > > print 'interno',j > > > What happens is that "j=j+2" inside IF does not change the loop > > counter ("j") as it would in C or Java, for example. > > > Am I missing something? > > > []'s > > Cesar > > What is your real intent here? This is how I understand it after > reading your post: you want to create a loop that steps by an > increment of 2. If that's the case, then: > > >>> for j in range(0,10,2): > > ... print j > ... > 0 > 2 > 4 > 6 > 8 > > would be a simple result. > > Cheers, > -Basilisk96 Actually I'm trying to convert a string to a list of float numbers: str = '53,20,4,2' to L = [53.0, 20.0, 4.0, 2.0] As some of you suggested, using while it works: ------------------------------------- L = [] file = ['5,1378,1,9', '2,1,4,5'] str='' for item in file: j=0 while(j<len(item)): while(item[j] != ','): str+=item[j] j=j+1 if(j>= len(item)): break if(str != ''): L.append(float(str)) str = '' j=j+1 print L ------------------------------------- But I'm not sure this is an elegant pythonic way of coding :-) Thanks for all suggestions! |
Re: Basic question
Cesar G. Miguel wrote:
> ------------------------------------- > L = [] > file = ['5,1378,1,9', '2,1,4,5'] > str='' > for item in file: > j=0 > while(j<len(item)): > while(item[j] != ','): > str+=item[j] > j=j+1 > if(j>= len(item)): break > > if(str != ''): > L.append(float(str)) > str = '' > > j=j+1 > > print L > But I'm not sure this is an elegant pythonic way of coding :-) Example: In [21]: '5,1378,1,9'.split(',') Out[21]: ['5', '1378', '1', '9'] So, instead of doing that while-based traversal and parsing of `item`, just split it like above, and use a for loop on it. It's much more elegant and pythonic. HTH, Karlo. |
Re: Basic question
On May 12, 3:09 pm, Karlo Lozovina <_karlo_@_mosor.net> wrote:
> Cesar G. Miguel wrote: > > ------------------------------------- > > L = [] > > file = ['5,1378,1,9', '2,1,4,5'] > > str='' > > for item in file: > > j=0 > > while(j<len(item)): > > while(item[j] != ','): > > str+=item[j] > > j=j+1 > > if(j>= len(item)): break > > > if(str != ''): > > L.append(float(str)) > > str = '' > > > j=j+1 > > > print L > > But I'm not sure this is an elegant pythonic way of coding :-) > > Example: > > In [21]: '5,1378,1,9'.split(',') > Out[21]: ['5', '1378', '1', '9'] > > So, instead of doing that while-based traversal and parsing of `item`, > just split it like above, and use a for loop on it. It's much more > elegant and pythonic. > > HTH, > Karlo. Great! Now it looks better :-) |
| All times are GMT. The time now is 07:49 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.