473,388 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Newbie question, list comprehension

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
4 1350
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
1
by: Mark Elston | last post by:
I recently stumbled over List Comprehension while reading the Python Cookbook. I have not kept up with the What's New sections in the online docs. :) Anyway, I thought I was following the...
7
by: Eelco Hoekema | last post by:
I'm trying to get a list of tuples, with each tuple consisting of a directory, and a list of files. I only want a tuple if and only if the filtered list of files is not empty. And, i want the list...
15
by: Darren Dale | last post by:
Hi, I need to replace the following loop with a list comprehension: res= for i in arange(10000): res=res+i In practice, res is a complex 2D numarray. For this reason, the regular output...
18
by: a | last post by:
can someone tell me how to use them thanks
12
by: beginner | last post by:
Hi All, How do I map a list to two lists with list comprehension? For example, if I have x=, ] What I want is a new list of list that has four sub-lists: , , , ]
5
by: Sengly | last post by:
Dear all, I am working with wordnet and I am a python newbie. I'd like to know how can I transfer a list below In : dog Out: to a list like this with python:
7
by: idiolect | last post by:
Hi all - Sorry to plague you with another newbie question from a lurker. Hopefully, this will be simple. I have a list full of RGB pixel values read from an image. I want to test each RGB band...
5
by: Pat | last post by:
I have written chunks of Python code that look this: new_array = for a in array: if not len( a ): continue new_array.append( a ) and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.