KBD

Keith Devens .com

Friday, July 4, 2008 Flag waving
All I want is to have my peace of mind. – Boston (Peace of Mind)

Archive: December 02, 2005

← December 01, 2005December 03, 2005 →

Daily link icon Friday, December 2, 2005

  1. I finally found that quote from the Chronicles of Narnia I'd been looking for.

       (0)
  2. I'm in Cambridge visiting my friend Greg. Woo!

       (0) Tags: [Personal]
  3. SpamIdentification - SpamLookup - Trac (via John Wiseman). Even goes so far as to do "dynamic proxy checking". "This is where the sender is actively probed to determine whether they are an open proxy or not." Neat.

       (0) Tags: [Programming]
  4. Jensen Harris: The Best Outlook Feature Ever (via Ned Batchelder). To try.

       (1)

Unusual Python list comprehensions

I needed to write a little code that would grab the filenames of all zip files out of a directory hierarchy. I immediately tried to write it as a big list comprehension and completely failed. So, I backed off a step to the following (some code in this post refomatted to not break my layout):

finalfiles = []
for (dir, subdirs, files) in os.walk("c:/temp/"):
    finalfiles.extend([
        os.path.join(dir, file)
        for file in files if file.lower().endswith(".zip")
    ])

I took a look at the list comprehension examples in the Python quick reference to figure out why I was having a problem. Here is their reference code (the two blocks are equivalent):

result = [expression for item1 in sequence1  if condition1
             for item2 in sequence2 ... for itemN in sequenceN
         ]

result = []
for item1 in sequence1:
    for item2 in sequence2:
        ...
        for itemN in sequenceN:
            if (condition1) and further conditions:
               result.append(expression)

So, first I converted my finalfiles.extend() to an append() form:

finalfiles = []
for (dir, subdirs, files) in os.walk("c:/temp/"):
    for file in files:
        if file.lower().endswith(".zip"):
            finalfiles.append(os.path.join(dir,file))

so that I could directly translate from the nested loop version to the list comprehension version, essentially doing textual substitution of item1, expression, and so on.

After doing the substitution, this is what you wind up with:

finalfiles = [
    os.path.join(dir,file)
    for (dir, subdirs, files) in os.walk("c:/temp/")
    for file in files if file.lower().endswith(".zip")
]

That seems backwards to my brain, which is why I didn't get it right the first time. I think this is really a case where doing a list comprehension is not clearer than doing at least part of it in a normal loop fashion.

Note: my brain feels like it should be this way:

finalfiles = [
    os.path.join(dir,file)
    for file in files if file.lower().endswith(".zip")
    for (dir, subdirs, files) in os.walk("c:/temp/")
]

which corresponds rather to:

result = [expression for item2 in sequence2
    for item1 in sequence1
]

I think that makes more sense. Which fits your brain?

  1. Comictastic "automatically grabs your favorite comics from the web, giving you unprecedented comic reading efficiency." That's neat. Mac only.

       (0)
  2. "These Java Programming Notes are written to fill in missing or weak topics in textbooks that I've taught from." (via).

       (0) Tags: [Programming]
← December 01, 2005December 03, 2005 →
July 2008
SunMonTueWedThuFriSat
 12345
6789101112
13141516171819
20212223242526
2728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 5 posts

Recent comments XML

new⇒Court rejects death penalty for raping children - Yahoo! News

Keith is not a person. I have this​on good authority. He's actually a​very,...

M. Bean: Jul 4, 2:05am

Girls, please don't get breast implants

> And no, you will not be receiving​a picture.

:-(...

Keith: Jul 2, 6:05am

Javascript clone function

This is a clever way to clone an​object if you are using YAHOO UI.​Same tec...

Antonio: Jul 1, 12:47pm

I hate Norton Antivirus

Oh just one other thing norton is​great at keeping people out of your​compu...

kevin.sands: Jul 1, 12:50am

Terminator 3 was awful

I think the biggest reason why T3​totally blew was because Edward​Furlong g...

76.167.172.64: Jun 29, 3:06am

Generated in about 0.063s.

(Used 7 db queries)

mobile phone