[Tutor] Troubles with lists and control flow

Luke Paireepinart rabidpoobear at gmail.com
Thu Oct 8 23:36:49 CEST 2009


Oops, accidentally replied off-list.

---------- Forwarded message ----------
From: Luke Paireepinart <rabidpoobear at gmail.com>
Date: Thu, Oct 8, 2009 at 3:36 PM
Subject: Re: [Tutor] Troubles with lists and control flow
To: Eduardo Vieira <eduardo.susan at gmail.com>




On Thu, Oct 8, 2009 at 2:42 PM, Eduardo Vieira <eduardo.susan at gmail.com>wrote:

> Hello I'm developing a script to compare two files, finding duplicate
> entries and matching which id of one csv file corresponds to the id of
> another csv file.
> The first version was working nice, but I wanted to postpone the
> writing to a file till the end and also make a correct csv file. The
> code is not so great and I expect to work with no more than 3000 lines
> of data in either file:
> So here is the inicial code. I hope it's not too long or complicated:
>

It's a little long to be in a message body, it'd have been nice if you
posted to pastebin and chosen Python so we could have seen it with syntax
highlighting and it would guarantee it doesn't mess up your indentation, but
it's fine for now.  Just keep that in mind for longer code samples.


> def inbv(currentline = None):
>    """writes a line of data when a date is found in BV"""
>    if currentline is None:
>        currentline = []
>    else:
>        currentline.append(item['USER_ID'])
>        currentline.append(row['CUS_NO'])
>        currentline.append(item['COMPANY'])
>        currentline.append(row['BVADDR1'])
>        currentline.append(item['ADDRESSLINEONE'])
>        currentline.append(row['BVADDRTELNO1'])
>        currentline.append(item['PHONE'])
>        currentline.append(row['BVCITY'])
>        currentline.append(item['CITY'])
>
>    return currentline
>
You don't want to do it like this.
What you're saying is:
"if they didn't pass in an argument to my function, create a new list and
return an empty list.  otherwise, if they did pass in a list, append an item
and return the new list."  What you really want to do is "if they didn't
pass in a list, create a new one.  Then append a value and return  the new
list."  There's a subtle difference, do you see it? You want to add an item
to the list whether or not they passed in a list in the first place, you
just want to initialize a new list first.  Think about your code and how you
can change it to do that.

At least I think that's what your issue is, I don't really understand your
code at all.

Also:

>
> def notinbv(currentline):
>    if currentline is None:
>        currentline = []


You should go ahead and define this one the same as the previous one (with
the optional currentline parameter) unless you left it out on purpose.

-Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091008/6ffdc675/attachment.htm>


More information about the Tutor mailing list