This is part of a short script I wrote to find out whether students had submitted any corrections to work in December or January.

import os, os.path

months = ['Dec', 'Jan']  # actually a raw_input()

files = get_all_files(root_dir)  # actually a call to os.path.walk()

dates = []

for i in files:
    for j in months:
         if j in i:  # filenames have date-stamp on them
             dates.append(i)
             break

In other words, dates ends up with all of the files whose names contain any of the items in months.

Can this be done with list comprehension?

Doesn't work:

dates =

Jeff

Recommended Answers

All 6 Replies

If you want to find file date,
stats = os.stat( filename )
print time.ctime( stats[8] )
prints 'Tue Dec 26 14:28:16 2006'
string.split, etc. yields the month, although I think you should also take the year into account.

can you try :

dates = [i for i in files for j in months if j in i  ]

Looks like Ghostdog is right ...

# test sample from user input
months = ['Dec', 'Jan']
# test sample from os.path.walk()
files = ['FebWork1,dat', 'JanWork3.dat', 'DecWork7.dat']  # test
dates = []
for i in files:
    for j in months:
         if j in i:
             dates.append(i)
             break
print dates
# ... or list comprehension ...
dates2 = [i for i in files for j in months if j in i  ]
print dates2
"""
my output -->
['JanWork3.dat', 'DecWork7.dat']
['JanWork3.dat', 'DecWork7.dat']
"""

Ah ... it seems obvious now. :lol:

Thanks,
Jeff

Looks like Ghostdog is right ...

# test sample from user input
months = ['Dec', 'Jan']
# test sample from os.path.walk()
files = ['FebWork1,dat', 'JanWork3.dat', 'DecWork7.dat']  # test
dates = []
for i in files:
    for j in months:
         if j in i:
             dates.append(i)
             break
print dates
# ... or list comprehension ...
dates2 = [i for i in files for j in months if j in i  ]
print dates2
"""
my output -->
['JanWork3.dat', 'DecWork7.dat']
['JanWork3.dat', 'DecWork7.dat']
"""

I sort of can see how list comprehension evolves from the individual for loops. Is there anything list comprehension can do that one could not write out with individual for loops?

Is there anything list comprehension can do that one could not write out with individual for loops?

Not in principle. However, in practice, list comprehension makes the code much more comprehensible. So: list comprehension does better code maintenance. :)

Mathematically,

mylist = [x for x in S if C]

is equivalent to

mylist = []
for x in S:
    if C:
       mylist.append(x)

so it must be the case that for loops can do anything list comprehensions can do.

But the expressive power ... wow!

average_salary = sum([p.salary for p in personnel])/len(personnel)

is exactly what I'm thinking, while

total = 0
for p in personnel:
   total += p.salary
average_salary = total / len(personnel)

was only what I was trained to think over time.

Jeff

* Error-checking not included. Salary information void where prohibited by law. p.salary may be negative or complex in some states.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.