Newbie question, list comprehension

J

Johannes Bauer

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 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 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
 
H

Hans Nowak

Johannes said:
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 for i in
range(0, 5)])


The % operator here wants a tuple with six arguments that are integers, not a
list. Try:

fmttime = "%04d-%02d-%02d %02d:%02d:%02d" % tuple(localtime for i in
range(6))
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?

In this case, you can just use a slice, as localtime is a tuple:

fmttime = "%04d-%02d-%02d %02d:%02d:%02d" % localtime[:6]

Hope this helps! ^_^
 
J

Johannes Bauer

Hans said:
In this case, you can just use a slice, as localtime is a tuple:

fmttime = "%04d-%02d-%02d %02d:%02d:%02d" % localtime[:6]

Hope this helps! ^_^

Ahh, how cool! That's *exactly* what I meant with "awesome Python magic" :)

Amazing language, I have to admit.

Regards,
Johannes
 
M

Mark Wooding

Johannes Bauer said:
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

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


To reduce typing, set

format = '%04d-%02d-%02d %02d:%02d:%02d'

Two problems.

* 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 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
anyway.)

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
tell:
'(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
tuple:
<type 'tuple'>

which is nice:
'2009-02-13 23:31:30'

But really what you wanted was probably
'2009-02-13 23:31:30'

-- [mdw]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top