Newbie question, list comprehension
mdw at distorted.org.uk
Sun Jun 8 15:11:50 CEST 2008
Johannes Bauer <dfnsonfsduifb at gmx.de> wrote:
> import time
> localtime = time.localtime(1234567890)
> fmttime = "%04d-%02d-%02d %02d:%02d:%02d" % (localtime, localtime,
> localtime, localtime, localtime, localtime)
> print fmttime
> fmttime = "%04d-%02d-%02d %02d:%02d:%02d" % ([localtime[i] for i in
> range(0, 5)])
To reduce typing, set
format = '%04d-%02d-%02d %02d:%02d:%02d'
* Firstly, range(0, 5) == [0, 1, 2, 3, 4], so it's not big enough.
Python tends to do this half-open-interval thing. Once you get used
to it, you'll find that it actually reduces the number of off-by-one
errors you make.
* Secondly, the result of a list comprehension is a list;
(Unsurprising, really, I know.) But the `%' operator only extracts
multiple arguments from a tuple, so you'd need to convert:
format % tuple(localtime[i] for i in xrange(6)]
(I've replaced range by xrange, which avoids building an intermediate
list, and the first argument to range or xrange defaults to zero
Another poster claimed that localtime returns a tuple. This isn't
correct: it returns a time.struct_time, which is not a tuple as you can
>>> '%s' % localtime
'(2009, 2, 13, 23, 31, 30, 4, 44, 0)'
This is one of those times when Python's duck typing fails -- string
formatting really wants a tuple of arguments, and nothing else will do.
But you can slice a time.struct_time, and the result /is/ a genuine
which is nice:
>>> format % localtime[:6]
But really what you wanted was probably
>>> time.strftime('%Y-%m-%d %H:%M:%S', localtime)
More information about the Python-list