[Tutor] Troubles with lists and control flow

Eduardo Vieira eduardo.susan at gmail.com
Fri Oct 9 17:36:23 CEST 2009


On Thu, Oct 8, 2009 at 3:36 PM, Luke Paireepinart
<rabidpoobear at gmail.com> wrote:
> 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
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>

Thanks Luke. I corrected my function, but I didn't have any good
result. In other words, the problem wasn't there.
The problem was with my control flow... I was using a "break" under
the else clause. I often get confused with control flow things...
Well, the 'else' clause on a for loop is always executed when there is
no "break" triggered in the loop. So, that "break" under the else was
not necessary ... and was not present in my first version too... copy
and paste error, I guess.

Eduardo
www.expresssignproducts.com


More information about the Tutor mailing list