944,107 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2626
  • Python RSS
Jan 17th, 2007
0

Can list comprehension do this?

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

Python Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Jan 17th, 2007
0

Re: Can list comprehension do this?

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.
Reputation Points: 741
Solved Threads: 693
Nearly a Posting Maven
woooee is offline Offline
2,307 posts
since Dec 2006
Jan 17th, 2007
0

Re: Can list comprehension do this?

can you try :

Python Syntax (Toggle Plain Text)
  1. dates = [i for i in files for j in months if j in i ]
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Jan 17th, 2007
0

Re: Can list comprehension do this?

Looks like Ghostdog is right ...
Python Syntax (Toggle Plain Text)
  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. """
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jan 17th, 2007
0

Re: Can list comprehension do this?

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

Thanks,
Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Jan 21st, 2007
0

Re: Can list comprehension do this?

Click to Expand / Collapse  Quote originally posted by vegaseat ...
Looks like Ghostdog is right ...
Python Syntax (Toggle Plain Text)
  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?
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Jan 21st, 2007
0

Re: Can list comprehension do this?

Click to Expand / Collapse  Quote originally posted by sneekula ...
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,

Python Syntax (Toggle Plain Text)
  1. mylist = [x for x in S if C]

is equivalent to
Python Syntax (Toggle Plain Text)
  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!

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

Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: A line class for coordinate geometry
Next Thread in Python Forum Timeline: File Compression





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC