[Tutor] Dynamically assign variable names to tuple objects

Hugo Arts hugo.yoshi at gmail.com
Tue Mar 1 20:18:15 CET 2011


On Tue, Mar 1, 2011 at 7:59 PM, Sean Carolan <scarolan at gmail.com> wrote:
>> I saw in your follow-up that you went straight for vars().  I really
>> don't think that's what you wish to use.  Get rid of vars(), he had
>> to go to jail.  Don't go visit vars() again for at least two months,
>> then maybe he'll be out on probation.
>
> Thanks Martin and Hugo.  As you can tell I'm no python guru.  Maybe I
> should take a step back and explain exactly what it is I'm trying to
> do.  I know this can be done quickly with awk or even perl but I want
> to get more practice with python.  So here's the basic idea:
>
> Take an arbitrary number of text files. Assume that each text file has
> the exact same number of lines.  Concatenate each line of each file
> with the corresponding lines of the other files and output the data.
> So in other words, the first line of output will be
> file1_line1+file2_line1+file3_line1, etc.
>
> I'll work on this some more and see what I can come up with.

My advice would be to go read up on the zip() function and the
str.join() function. Then, if you are using python 2.x, go find
itertools.izip. It does the same thing as zip but it's more memory
efficient. With those two you can do it in about two lines or so (and
maybe a few for set up and clarity and such).

If you get stuck, don't hesitate to ask.

HTH,
Hugo


More information about the Tutor mailing list