easier solution possible to find a stringpattern in a list?
Otto Tronarp
otttr440 at student.liu.se
Sat Jan 4 08:30:36 EST 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"
> else:
> 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
> filesystem.
>
> thanks for any help
> chris
How about this
import glob
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
else:
print "not in the list"
Otto
More information about the Python-list
mailing list