[Tutor] Dynamically assign variable names to tuple objects

Steven D'Aprano steve at pearwood.info
Tue Mar 1 22:37:50 CET 2011


Emile van Sebille wrote:
> On 3/1/2011 11:49 AM Sean Carolan said...
>>> 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).
>>
>> This is what I've got so far:
>>
>> import sys
>> myfiles = sys.argv[1:]
>> for i in zip(open(myfiles[0]), open(myfiles[1]), open(myfiles[2])):
>>      print " ".join(i)
>>
>> How would you:
>>
>> 1.  zip an arbitrary number of files in this manner?  I hard-coded it
>> to do only three.
> 
> One way:
> 
> for i in zip([ open(filename) for filename in myfiles ])

Almost, you need to expand the list:

zip( *[open(filename) for filename in myfiles] )

which is equivalent to pseudocode:

zip(open(myfiles[0]), open(myfiles[1]), ..., open(myfiles[N]))


Another way is:

zip(*map(open, myfiles))

Again, you need to expand the list using *.



>> 2.  Strip out trailing spaces and line breaks from the lines in each 
>> file?
> 
> Convert the file contents before zip'ing -- so add
> 
> def cleanedup(filename):
>     return [ line.strip() for line in open(filename) ]

Better to do the stripping on demand, rather than all up front. 
Especially if the files are huge. Turn the list comprehension [...] into 
a generator expression (...):

def cleanedup(filename):
     return (line.strip() for line in open(filename))


> Then your loop looks like:
> 
> for i in zip([ cleanedup(filename) for filename in myfiles ])



-- 
Steven



More information about the Tutor mailing list