Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   For Counter Variable (http://www.velocityreviews.com/forums/t952555-for-counter-variable.html)

jimbo1qaz 09-23-2012 04:36 PM

For Counter Variable
 
Am I missing something obvious, or do I have to manually put in a counter in the for loops? That's a very basic request, but I couldn't find anything in the documentation.

Rodrick Brown 09-23-2012 04:45 PM

Re: For Counter Variable
 
On Sep 23, 2012, at 12:42 PM, jimbo1qaz <jimmyli1528@gmail.com> wrote:

> Am I missing something obvious, or do I have to manually put in a counter in the for loops? That's a very basic request, but I couldn't find anything in the documentation.


for idx in <list of elm>: print (idx)

i.e.. for idx in range(10): print(idx)

> --
> http://mail.python.org/mailman/listinfo/python-list


Chris Angelico 09-23-2012 04:52 PM

Re: For Counter Variable
 
On Mon, Sep 24, 2012 at 2:36 AM, jimbo1qaz <jimmyli1528@gmail.com> wrote:
> Am I missing something obvious, or do I have to manually put in a counter in the for loops? That's a very basic request, but I couldn't find anything in the documentation.


You mean, if you want the indices as well as the values? Try the
enumerate() function:

my_list = ["foo", "bar", "quux"]
for idx,val in enumerate(my_list):
print("Element "+str(idx)+" is: "+val)

ChrisA

jimbo1qaz 09-23-2012 05:45 PM

Re: For Counter Variable
 
On Sunday, September 23, 2012 9:36:19 AM UTC-7, jimbo1qaz wrote:
> Am I missing something obvious, or do I have to manually put in a counter in the for loops? That's a very basic request, but I couldn't find anything in the documentation.


Ya, they should really give a better way, but for now, enumerate works pretty well.

Steven D'Aprano 09-23-2012 10:54 PM

Re: For Counter Variable
 
On Sun, 23 Sep 2012 10:45:53 -0700, jimbo1qaz wrote:

> On Sunday, September 23, 2012 9:36:19 AM UTC-7, jimbo1qaz wrote:
>> Am I missing something obvious, or do I have to manually put in a
>> counter in the for loops? That's a very basic request, but I couldn't
>> find anything in the documentation.

>
> Ya, they should really give a better way, but for now, enumerate works
> pretty well.


Define "a better way". What did you have in mind that would work better?



--
Steven

Tim Chase 09-23-2012 11:29 PM

Re: For Counter Variable
 
On 09/23/12 17:54, Steven D'Aprano wrote:
> On Sun, 23 Sep 2012 10:45:53 -0700, jimbo1qaz wrote:
>> On Sunday, September 23, 2012 9:36:19 AM UTC-7, jimbo1qaz wrote:
>>> Am I missing something obvious, or do I have to manually put in a
>>> counter in the for loops? That's a very basic request, but I couldn't
>>> find anything in the documentation.

>>
>> Ya, they should really give a better way, but for now, enumerate works
>> pretty well.

>
> Define "a better way". What did you have in mind that would work better?


I can only imagine jimbo1qaz intended "a more C-like way". blech.

I **far** prefer The Python Way™. The vast majority of the time,
I'm looping over some iterable where indices would only get in the
way of readability. Tuple-unpacking the results of enumerate() is
an elegant way of getting both the items+indices on the seldom
occasion I need the index too (though I'm minorly miffed that
enumerate()'s starting-offset wasn't back-ported into earlier 2.x
versions and have had to code around it for 1-based indexing; either
extra "+1"s or whip up my own simple enumerate() generator).

-tkc



Tim Chase 09-24-2012 12:05 AM

Re: For Counter Variable
 
On 09/23/12 18:52, Alec Taylor wrote:
> You can always use a counter if you don't like our fancy for-each loops;
>
> foolist = [1,24,24,234,23,423,4]
> for i in xrange(len(foolist)):
> print foolist[i]


http://www.seas.upenn.edu/~lignos/py_antipatterns.html

The first one on the list of anti-patterns is doing exactly this.
Just don't. Ewww. Inefficient, ugly, and harder to read.

Part of learning to write in Python is, well, learning to write
*Python*, not {C,C++,Java,PHP}-in-Python.

-tkc





Mark Lawrence 09-24-2012 01:03 AM

Re: For Counter Variable
 
On 24/09/2012 01:05, Tim Chase wrote:
> On 09/23/12 18:52, Alec Taylor wrote:
>> You can always use a counter if you don't like our fancy for-each loops;
>>
>> foolist = [1,24,24,234,23,423,4]
>> for i in xrange(len(foolist)):
>> print foolist[i]

>
> http://www.seas.upenn.edu/~lignos/py_antipatterns.html
>
> The first one on the list of anti-patterns is doing exactly this.
> Just don't. Ewww. Inefficient, ugly, and harder to read.
>
> Part of learning to write in Python is, well, learning to write
> *Python*, not {C,C++,Java,PHP}-in-Python.
>
> -tkc
>


Maybe my mind is rather more warped than I thought it was, but my first
impression was that foolist was a play on foolish.

I also like the anti-pattern on the link namely:-

for (index, value) in enumerate(alist):
print index, value

Fancy wasting time, money and effort typing those unnecessary round
brackets.

--
Cheers.

Mark Lawrence.


Ethan Furman 09-24-2012 10:09 PM

Re: For Counter Variable
 
jimbo1qaz wrote:
> On Sunday, September 23, 2012 9:36:19 AM UTC-7, jimbo1qaz wrote:
>> Am I missing something obvious, or do I have to manually put in a counter in the for loops? That's a very basic request, but I couldn't find anything in the documentation.

>
> Ya, they should really give a better way, but for now, enumerate works pretty well.


ROFLOL!!

I look forward to the day when you look back on that statement and
think, "Wow, I've come a long way!"

~Ethan~

Dwight Hutto 09-24-2012 10:26 PM

Re: For Counter Variable
 
On Mon, Sep 24, 2012 at 6:09 PM, Ethan Furman <ethan@stoneleaf.us> wrote:
> jimbo1qaz wrote:
>>
>> On Sunday, September 23, 2012 9:36:19 AM UTC-7, jimbo1qaz wrote:
>>>
>>> Am I missing something obvious, or do I have to manually put in a counter
>>> in the for loops? That's a very basic request, but I couldn't find anything
>>> in the documentation.

>>
>>
>> Ya, they should really give a better way, but for now, enumerate works
>> pretty well.

>
>
> ROFLOL!!
>
> I look forward to the day when you look back on that statement and think,
> "Wow, I've come a long way!"
>


It's a function usage. Not to be too serious, there are usually
simpler solutions, and built in functions.

But you usually sticks with what works, and seems timely in return of
data output

--
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com


All times are GMT. The time now is 09:25 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.