I don't think so. Not as a Python concept, but it looks sensible in
your example. However, why would enumerate produce a line number? How
would one know that it does? Ah, I see. enumerate produces a tuple
which has the index and a list. It appears the only place this can be
used is in a for? And, of course (devoid of an example): >>> help(enumerate) class enumerate(object) | enumerate(iterable) -> iterator for index, value of iterable | | Return an enumerate object. iterable must be an other object that supports | iteration. The enumerate object yields pairs containing a count (from | zero) and a value yielded by the iterable argument. enumerate is useful | for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ... ... Kent Johnson wrote: On Sun, Feb 15, 2009 at 2:47 PM, Wayne Watson <sierra_mtnv...@sbcglobal.net> wrote:I'm still looking for an explanation of "for (line_cnt, each_line) in enumerate(input_file)". Why the tuple? Apparently, line_count gets a line number, and each_line gets the string of text.Do you know about sequence unpacking? In an assignment statement, when the right side is a sequence, the left side can be a list of variables of the same length as the sequence. Then each sequence element is assigned to one variable. For example,In [24]: item = (0, 'Spring') In [25]: i, season = item In [26]: i Out[26]: 0 In [28]: season Out[28]: 'Spring' This can be used in a for loop, too, if the items are sequences. That is what is commonly done with enumerate(). Kent --
Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) Shhhh, quiet. I'm thinking about filling this space. Web Page: <www.speckledwithstars.net/> |
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor