443,898 Members | 1,198 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

Basic question

 P: n/a 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 May 12 '07 #1
22 Replies

 P: n/a 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. May 12 '07 #2

 P: n/a On May 12, 5:18 pm, "Cesar G. Miguel" ; ; ) " you have to use while in Python: while : Of course in most case it would not be the "pythonic" way of doing it :) -- Arnaud May 12 '07 #3

 P: n/a 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. May 12 '07 #4

 P: n/a "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 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 May 12 '07 #5

 P: n/a On May 12, 12:18 pm, "Cesar G. Miguel" >for j in range(0,10,2): .... print j .... 0 2 4 6 8 would be a simple result. Cheers, -Basilisk96 May 12 '07 #6

 P: n/a On May 12, 2:45 pm, Basilisk96 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)): 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! May 12 '07 #7

 P: n/a 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)): 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. May 12 '07 #8

 P: n/a On May 12, 3:09 pm, Karlo Lozovina <_karlo_@_mosor.netwrote: 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)): 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 :-) May 12 '07 #9

 P: n/a 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] str="53,20,4,2" map(lambda s: float(s), str.split(',')) Last expression returns: [53.0, 20.0, 4.0, 2.0] -- Happy Hacking. Dmitry "Sphinx" Dzhus http://sphinx.net.ru May 12 '07 #10

 P: n/a On 2007-05-12, Cesar G. Miguel >str = '53,20,4,2' >>[float(w) for w in str.split(',')] [53.0, 20.0, 4.0, 2.0] >>map(float,str.split(',')) [53.0, 20.0, 4.0, 2.0] -- Grant Edwards grante Yow! I want you to at MEMORIZE the collected visi.com poems of EDNA ST VINCENT MILLAY... BACKWARDS!! May 12 '07 #11

 P: n/a On 2007-05-12, Dmitry Dzhus

 P: n/a On May 12, 3:40 pm, Dmitry Dzhus

 P: n/a "Cesar G. Miguel" ; //Java 1.5 addition I think. for(int x=0,x Am I missing something? []'s Cesar -- Kirk Job Sluder May 12 '07 #14

 P: n/a Cesar G. Miguel

 P: n/a On May 12, 6:18 pm, "Cesar G. Miguel"

 P: n/a On May 13, 12:13 am, a...@mac.com (Alex Martelli) wrote: As somebody else alredy pointed out, the lambda is supererogatory (to say the least). What a wonderful new word! I did not know what supererogatory meant, and hoped it had nothing to do with Eros :-) Answers.com gave me a meaning synonymous with superfluous, which I think is what was meant here, but Chambers gave a wonderful definition where they say it is from the RC Church practice of doing more devotions than are necessary so they can be 'banked' for distribution to others (I suspect, that in the past it may have been for a fee or a favour). Supererogatory, my word of the day. - Paddy P.S; http://www.chambersharrap.co.uk/cham...ry+&title=21st May 13 '07 #17

 P: n/a Paddy ). Supererogatory, my word of the day. Glad you liked it!-) Alex May 13 '07 #18

 P: n/a On May 12, 8:13 pm, a...@mac.com (Alex Martelli) wrote: Cesar G. Miguel

 P: n/a En Sat, 12 May 2007 20:13:48 -0300, Alex Martelli -------------------L = []file = ['5,1378,1,9', '2,1,4,5']str=''for item in file: L.append([float(n) for n in item.split(',')]) The assignment to str is useless (in fact potentially damaging because you're hiding a built-in name). L = [float(n) for item in file for n in item.split(',')] is what I'd call Pythonic, personally (yes, the two for clauses need to be in this order, that of their nesting). But that's not the same as requested - you get a plain list, and the original was a list of lists: L = [[float(n) for n in item.split(',')] for item in file] And thanks for my "new English word of the day": supererogatory :) -- Gabriel Genellina May 14 '07 #20

 P: n/a Gabriel Genellina = len(item)): break if(str != ''): L.append(float(str)) str = '' j=j+1 print L This makes L a list of floats, DEFINITELY NOT a list of lists. And thanks for my "new English word of the day": supererogatory :) You're welcome! Perhaps it's because I'm not a native speaker of English, but I definitely do like to widen my vocabulary (and others'). Alex May 14 '07 #21

 P: n/a En Mon, 14 May 2007 02:05:47 -0300, Alex Martelli But that's not the same as requested - you get a plain list, and theoriginal was a list of lists: Are we talking about the same code?! What I saw at the root of this subthread was, and I quote: [...code painfully building a plain list...] Oh, sorry, I got confused with another reply then. >And thanks for my "new English word of the day": supererogatory :) You're welcome! Perhaps it's because I'm not a native speaker of English, but I definitely do like to widen my vocabulary (and others'). Me too! -- Gabriel Genellina May 14 '07 #22

 P: n/a Dmitry Dzhus skrev: >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] str="53,20,4,2" map(lambda s: float(s), str.split(',')) Last expression returns: [53.0, 20.0, 4.0, 2.0] The lambda is not needed there, as float is a callable. map(float, str.split(',')) -- hilsen/regards Max M, Denmark http://www.mxm.dk/ IT's Mad Science May 14 '07 #23

This discussion thread is closed

Replies have been disabled for this discussion.