| | |
Can list comprehension do this?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
This is part of a short script I wrote to find out whether students had submitted any corrections to work in December or January.
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
Python Syntax (Toggle Plain Text)
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 = [i for i in files if j in i for j in months]
Jeff
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 40
can you try :
Python Syntax (Toggle Plain Text)
dates = [i for i in files for j in months if j in i ]
Looks like Ghostdog is right ...
Python Syntax (Toggle Plain Text)
# 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'] """
May 'the Google' be with you!
•
•
•
•
Looks like Ghostdog is right ...
Python Syntax (Toggle Plain Text)
# 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'] """
No one died when Clinton lied.
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
•
•
•
•
Is there anything list comprehension can do that one could not write out with individual for loops?

Mathematically,
Python Syntax (Toggle Plain Text)
mylist = [x for x in S if C]
is equivalent to
Python Syntax (Toggle Plain Text)
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!
Python Syntax (Toggle Plain Text)
average_salary = sum([p.salary for p in personnel])/len(personnel)
Python Syntax (Toggle Plain Text)
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.
Last edited by jrcagle; Jan 21st, 2007 at 3:19 pm.
![]() |
Similar Threads
- Two List Difference (Python)
- reading a list (Python)
- How to map one list onto another using a dictionary?? (Python)
- Python's Profiler (Python)
- List Comprehension (Python)
Other Threads in the Python Forum
- Previous Thread: A line class for coordinate geometry
- Next Thread: File Compression
| Thread Tools | Search this Thread |
accessdenied advanced application argv beginner change color command convert csv cursor def dictionary digital dynamic dynamically edit editing enter event examples excel file float format frange function google gui homework i/o import input jaunty java keyboard lapse line linux list lists loop microphone mouse movingimageswithpygame newb number numbers numeric obexftp output parameters parsing path port prime programming projects py2exe pygame pygtk pyopengl python random recursion remote return reverse scrolledtext session simple skinning smtp sprite stderr string strings subprocess syntax table tennis terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode unit urllib urllib2 variable voip web-scrape windows wxpython






