开发者

Python: Passing functions with arguments to a built-in function?

like this question I want to pass a function with arguments. But I want to pass it to built-in functions.

Example:

files = [ 'hey.txt', 'hello.txt', 'goodbye.jpg', 'howdy.gif' ]

def filterex(path, ex):
  pat = r'.+\.(' + ex + ')$'
  match = re.search(pat, path)         
  return match and match.group(1)开发者_Go百科 == ex) 

I could use that code with a for loop and an if statement but it's shorter and maybe more readable to use filter(func, seq). But if I understand correctly the function you use with filter only takes one argument which is the item from the sequence.

So I was wondering if it's possible to pass more arguments?


def make_filter(ex):
    def do_filter(path):
        pat = r'.+\.(' + ex + ')$'
        match = re.search(pat, path)
        return match and match.group(1) == ex
    return do_filter

filter(make_filter('txt'), files)

Or if you don't want to modify filterex:

filter(lambda path: filterex(path, 'txt'), files)

You could use a list comprehension, as suggested by gnibbler:

[path for path in files if filterex(path, 'txt')]

You could also use a generator comprehension, which might be particularly useful if you had a large list:

(path for path in files if filterex(path, 'txt'))


Here is a list comprehension that does the same thing

import os
[f for f in files if os.path.splitext(f)[1]=="."+ex]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜