Re: Counting iterations

2005-04-11 Thread Andrew Dalke
Derek Basch wrote: > Interesting stuff Andrew. I do generally avoid string concantination > for the reason listed in the Python Performance Tips but your analysis > kinda puts that in question. Thanks. It was interesting for me to. I hadn't looked at the implementation for string % before and wa

Re: Counting iterations

2005-04-10 Thread Terry Reedy
"runes" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > [Andrew Dalke] >> I therefore disagree with the idea that simple >> string concatenation is always to be eschewed >> over string interpolation. String cat (+) is fine for joining a few short strings. > Andrew, what you writ

Re: Counting iterations

2005-04-10 Thread Derek Basch
Interesting stuff Andrew. I do generally avoid string concantination for the reason listed in the Python Performance Tips but your analysis kinda puts that in question. Such a dense discussion for me just trying to find the enumerate() function :). I guess that is why the python list is so great. Y

Re: Counting iterations

2005-04-09 Thread runes
[Andrew Dalke] > I therefore disagree with the idea that simple > string concatenation is always to be eschewed > over string interpolation. Andrew, what you write makes sense. I've never really tested it, just read it several places, fx here: http://www.python.org/moin/PythonSpeed/PerformanceTip

Re: Counting iterations

2005-04-09 Thread Steven Bethard
Andrew Dalke wrote: "pet#%i" % (i+1) (NOTE: most times that's written %d instead of %i) Any reason to prefer %d over %i? The library reference seems to suggest that they're the same thing[1]. I've typically used %i since I can remember it from the int type, like I can remember %f from the floa

Re: Counting iterations

2005-04-09 Thread Andrew Dalke
runes wrote: > You should avoid the "a" + "b" + "c" -kind of concatenation. As strings > at immutable in Python you actually makes copies all the time and it's > slow! The OP wrote print "pet" + "#" + num_pets (properly str(num_pets) ) You recommended the "alternative used in Steven Bethard

Re: Counting iterations

2005-04-09 Thread runes
You should avoid the "a" + "b" + "c" -kind of concatenation. As strings at immutable in Python you actually makes copies all the time and it's slow! The alternative used in Steven Bethard's example is preferable. -- http://mail.python.org/mailman/listinfo/python-list

Re: Counting iterations

2005-04-09 Thread Fredrik Lundh
Derek Basch wrote: > ooops you are right. Should have been: > > pets = ["cat", "dog", "bird"] > num_pets = 0 > for i in pets: >num_pets += 1 >print "pet" + "#" + num_pets Traceback (most recent call last): File "example.py", line 5, in ? print "pet" + "#" + num_pets TypeError: canno

Re: Counting iterations

2005-04-08 Thread Derek Basch
ooops you are right. Should have been: pets = ["cat", "dog", "bird"] num_pets = 0 for i in pets: num_pets += 1 print "pet" + "#" + num_pets That's the problem with one offs. I don't read them :). -- http://mail.python.org/mailman/listinfo/python-list

Re: Counting iterations

2005-04-08 Thread Will McGugan
Derek Basch wrote: Is there a better way to count iterations that this?: pets = 0 for i in pets: pets += 1 print "pet" + "#" + pets You can use 'enumerate' to get the index, but the code above wont work - you are trying to iterate over a non-sequence. Will McGugan -- "".join( [ {'@':'@','

Re: Counting iterations

2005-04-08 Thread Steven Bethard
Derek Basch wrote: Is there a better way to count iterations that this?: pets = 0 for i in pets: pets += 1 print "pet" + "#" + pets for i, pet in enumerate(pets): print 'pet#%i' % (i + 1) STeVe -- http://mail.python.org/mailman/listinfo/python-list

Counting iterations

2005-04-08 Thread Derek Basch
Is there a better way to count iterations that this?: pets = 0 for i in pets: pets += 1 print "pet" + "#" + pets Thanks, Derek Basch -- http://mail.python.org/mailman/listinfo/python-list