Python Tips for filtering strings

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.