By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

Newbie question, list comprehension

P: n/a
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 <47***********************@news.freenet.de>
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Johannes Bauer wrote:
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)])
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[i] 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! ^_^

--
Hans Nowak (zephyrfalcon at gmail dot com)
http://4.flowsnake.org/
Jun 27 '08 #2

P: n/a
Hans Nowak schrieb:
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

--
"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 <47***********************@news.freenet.de>
Jun 27 '08 #3

P: n/a
Johannes Bauer wrote:
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
You should look at time.strftime. It will do the formatting for you so you
don't have to do it manually as you have.

-Larry
Jun 27 '08 #4

P: n/a
Johannes Bauer <df***********@gmx.dewrote:
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[i] 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[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
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:
>>'%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
tuple:
>>type(localtime[:6])
<type 'tuple'>

which is nice:
>>format % localtime[:6]
'2009-02-13 23:31:30'

But really what you wanted was probably
>>time.strftime('%Y-%m-%d %H:%M:%S', localtime)
'2009-02-13 23:31:30'

-- [mdw]
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.