Re: for i in range() anti-pattern [was Re: trouble writing results to files]

2006-11-30 Thread Roberto Bonvallet
Steven D'Aprano wrote: > On Wed, 29 Nov 2006 17:00:30 +0100, Fredrik Lundh wrote: > >> Neil Cerutti wrote: >> BTW, iterating over range(len(a)) is an anti-pattern in Python. >>> >>> Unless you're modifying elements of a, surely? >> >> and needs to run on a Python version that doesn't suppo

for i in range() anti-pattern [was Re: trouble writing results to files]

2006-11-29 Thread Steven D'Aprano
On Wed, 29 Nov 2006 17:00:30 +0100, Fredrik Lundh wrote: > Neil Cerutti wrote: > >>> BTW, iterating over range(len(a)) is an anti-pattern in Python. >> >> Unless you're modifying elements of a, surely? > > and needs to run on a Python version that doesn't support enumerate. This isn't meant as

Re: trouble writing results to files

2006-11-29 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: > I can try that. Is using range(len(a)) a bad solution in the sense > that its likely to create an unexpected error? Or because there is a > more efficient way to accomplish the same thing? for-in uses an internal index counter to fetch items from the sequence, so

Re: trouble writing results to files

2006-11-29 Thread lisa . engblom
Roberto Bonvallet wrote: > [EMAIL PROTECTED] wrote: > > import csv > > output = csv.writer(open('/Python25/working/output.csv', 'a')) > > a = ["apple", "cranberry", "tart"] > > for elem in range(len(a)): > >output.writerow(a[elem]) > > output.writerow expects a sequence as an argument. You ar

Re: trouble writing results to files

2006-11-29 Thread Fredrik Lundh
Neil Cerutti wrote: >> BTW, iterating over range(len(a)) is an anti-pattern in Python. > > Unless you're modifying elements of a, surely? and needs to run on a Python version that doesn't support enumerate. -- http://mail.python.org/mailman/listinfo/python-list

Re: trouble writing results to files

2006-11-29 Thread Neil Cerutti
On 2006-11-29, Roberto Bonvallet <[EMAIL PROTECTED]> wrote: > Neil Cerutti wrote: >> On 2006-11-29, Roberto Bonvallet <[EMAIL PROTECTED]> wrote: >>> BTW, iterating over range(len(a)) is an anti-pattern in Python. >> >> Unless you're modifying elements of a, surely? > > enumerate is your friend :)

Re: trouble writing results to files

2006-11-29 Thread Roberto Bonvallet
Neil Cerutti wrote: > On 2006-11-29, Roberto Bonvallet <[EMAIL PROTECTED]> wrote: >> BTW, iterating over range(len(a)) is an anti-pattern in Python. > > Unless you're modifying elements of a, surely? enumerate is your friend :) for n, item in enumerate(a): if f(item): a[n

Re: trouble writing results to files

2006-11-29 Thread Neil Cerutti
On 2006-11-29, Roberto Bonvallet <[EMAIL PROTECTED]> wrote: > BTW, iterating over range(len(a)) is an anti-pattern in Python. Unless you're modifying elements of a, surely? -- Neil Cerutti You can't give him that cutback lane. He's so fast, and he sees it so well. He can also run away from you i

Re: trouble writing results to files

2006-11-29 Thread Roberto Bonvallet
[EMAIL PROTECTED] wrote: > import csv > output = csv.writer(open('/Python25/working/output.csv', 'a')) > a = ["apple", "cranberry", "tart"] > for elem in range(len(a)): >output.writerow(a[elem]) output.writerow expects a sequence as an argument. You are passing a string, which is a sequence o