473,289 Members | 1,952 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,289 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 1344
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.