Newbie question, list comprehension

Johannes Bauer dfnsonfsduifb at gmx.de
Sat Jun 7 00:03:47 CEST 2008


Hello group,

I'm currently doing something like this:

import time
localtime = time.localtime(1234567890)
fmttime = "%04d-%02d-%02d %02d:%02d:%02d" % (localtime[0], localtime[1], 
localtime[2], localtime[3], localtime[4], localtime[5])
print fmttime

For the third line there is, I suppose, some awesome python magic I 
could use with list comprehensions. I tried:

fmttime = "%04d-%02d-%02d %02d:%02d:%02d" % ([localtime[i] for i in 
range(0, 5)])

But that didn't work:

Traceback (most recent call last):
   File "./test.py", line 8, in ?
     fmttime = "%04d-%02d-%02d %02d:%02d:%02d" % ([localtime[i] for i in 
range(0, 5)])
TypeError: int argument required

As it appearently passed the while list [2009, 02, 14, 0, 31, 30] as the 
first parameter which is supposed to be substituted by "%04d". Is there 
some other way of doing it?

Thanks a lot,
Regards,
Johannes

-- 
"Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
reicht zu wissen, daß andere es besser können und andere es auch
besser machen um einen Vergleich zu bringen."     -     Wolfgang Gerber
       in de.sci.electronics <47fa8447$0$11545$9b622d9e at news.freenet.de>



More information about the Python-list mailing list