Can list comprehension do this?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Can list comprehension do this?

 
0
  #1
Jan 17th, 2007
This is part of a short script I wrote to find out whether students had submitted any corrections to work in December or January.

  1. import os, os.path
  2.  
  3. months = ['Dec', 'Jan'] # actually a raw_input()
  4.  
  5. files = get_all_files(root_dir) # actually a call to os.path.walk()
  6.  
  7. dates = []
  8.  
  9. for i in files:
  10. for j in months:
  11. if j in i: # filenames have date-stamp on them
  12. dates.append(i)
  13. 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 = [i for i in files if j in i for j in months]

Jeff
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,035
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 292
woooee woooee is offline Offline
Veteran Poster

Re: Can list comprehension do this?

 
0
  #2
Jan 17th, 2007
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.
Last edited by woooee; Jan 17th, 2007 at 1:20 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Can list comprehension do this?

 
0
  #3
Jan 17th, 2007
can you try :

  1. dates = [i for i in files for j in months if j in i ]
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,067
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 937
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Can list comprehension do this?

 
0
  #4
Jan 17th, 2007
Looks like Ghostdog is right ...
  1. # test sample from user input
  2. months = ['Dec', 'Jan']
  3. # test sample from os.path.walk()
  4. files = ['FebWork1,dat', 'JanWork3.dat', 'DecWork7.dat'] # test
  5. dates = []
  6. for i in files:
  7. for j in months:
  8. if j in i:
  9. dates.append(i)
  10. break
  11. print dates
  12. # ... or list comprehension ...
  13. dates2 = [i for i in files for j in months if j in i ]
  14. print dates2
  15. """
  16. my output -->
  17. ['JanWork3.dat', 'DecWork7.dat']
  18. ['JanWork3.dat', 'DecWork7.dat']
  19. """
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Can list comprehension do this?

 
0
  #5
Jan 17th, 2007
Ah ... it seems obvious now. :lol:

Thanks,
Jeff
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Can list comprehension do this?

 
0
  #6
Jan 21st, 2007
Originally Posted by vegaseat View Post
Looks like Ghostdog is right ...
  1. # test sample from user input
  2. months = ['Dec', 'Jan']
  3. # test sample from os.path.walk()
  4. files = ['FebWork1,dat', 'JanWork3.dat', 'DecWork7.dat'] # test
  5. dates = []
  6. for i in files:
  7. for j in months:
  8. if j in i:
  9. dates.append(i)
  10. break
  11. print dates
  12. # ... or list comprehension ...
  13. dates2 = [i for i in files for j in months if j in i ]
  14. print dates2
  15. """
  16. my output -->
  17. ['JanWork3.dat', 'DecWork7.dat']
  18. ['JanWork3.dat', 'DecWork7.dat']
  19. """
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?
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Can list comprehension do this?

 
0
  #7
Jan 21st, 2007
Originally Posted by sneekula View Post
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,

  1. mylist = [x for x in S if C]

is equivalent to
  1. mylist = []
  2. for x in S:
  3. if C:
  4. mylist.append(x)

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

But the expressive power ... wow!

  1. average_salary = sum([p.salary for p in personnel])/len(personnel)
is exactly what I'm thinking, while

  1. total = 0
  2. for p in personnel:
  3. total += p.salary
  4. 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.
Last edited by jrcagle; Jan 21st, 2007 at 3:19 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC