The map function in Python is very useful, both in character conversion and character traversal, and now it's there, what kind of surprise will it bring us? Do you want to tell us that map is very good, and later to find it to play it?
Specific examples of
We need to traverse the directory, including subdirectories (haha), to find all the suffixes: rmvb, AVI, PMP files. (Oh, my God!) What are you doing? This is my privacy AH ~ ~)
The code is as follows:
Import OS def anytrue (predicate, sequence): Return True in map (predicate, sequence) def filterfiles (folder, exts): For fil Ename in Os.listdir (folder): If Os.path.isdir (folder + '/' + filename): Filterfiles (folder + '/' + filename, exts) elif an Ytrue (Filename.endswith, exts): print FileName exts = ['. rmvb ', '. avi ', '. PMP '] filterfiles ('/media/personal/movie ', EXTS)
Output results
Let's see what's Good:
[66 film www.66ys.cn] lost fourth season 04.RMVB
[Lost. 4th season]. Lost.S04E00.rmvb
[Lost lost fourth season] [No. 02 Episode] [English subtitled].rmvb
"Lost lost fourth season" No. 05 episode [Chinese subtitled].rmvb
"Lost lost fourth season" No. 06 episode [Chinese subtitled].rmvb
"Lost lost fourth season" No. 07 episode [Chinese subtitled].rmvb
Godsend 2nd Quarter 01.RMVB
Godsend 2nd Quarter 02.RMVB
Godsend 2nd Quarter 03.RMVB
Godsend 2nd Quarter 04.RMVB
Godsend 2nd Quarter 05.RMVB
Movie Empire (bbs.cnxp.com). Beautiful Mind. A.beautiful.mind.2001.cd1.rmvb
( ... Too much, not all output come ~ ~)
Extended
In cookbook, a itertools.imap is provided to enable filtering of strings. IMAP and map are different, IMAP returns a iteration object, and map returns a list object. The code is as follows:
Import Itertools def anytrue (predicate, sequence): Return True in Itertools.imap (predicate, sequence) def endsWith (S, *end Ings): Return anytrue (S.endswith, endings) IMAP equivalent to: DEF IMAP (function, *iterables): Iterables = Map (iter, Iterables) whil e True:args = [I.next () for i in Iterables] if function is None:yield tuple (args) Else:yield function (*args)