easier solution possible to find a stringpattern in a list?
otttr440 at student.liu.se
Sat Jan 4 14:30:36 CET 2003
On Sat, 2003-01-04 at 14:03, christof hoeke wrote:
> files = [...]
> pattern = "*.xsl"
> number = len( [f for f in files if f.endswith(pattern)] )
> if number > 0:
> print number + " times in the list"
> print "not in the list"
> is there already a function in the standard modules that can do something
> like this?
> maybe even one which can handle more generalized patterns?
> i guess i am looking for a version of glob which handles a list and not the
> thanks for any help
How about this
files = ['file1.xsl', 'file2.xxx', 'file3.xsl', 'file4.xsl']
pattern = '*.xsl'
number = len(glob.fnmatch.filter(files, pattern))
if number > 0:
print "%s times in the list" % number
print "not in the list"
More information about the Python-list