如何动态传递谓词到过滤函数?

2 投票
1 回答
1234 浏览
提问于 2025-04-17 19:18

我有一个叫做 eval_params 的字典,它的内容大概是这样的:

In [1]: eval_params
Out[1]: 
{1: {'CLF': 'EMNB',
  'EM': 'False',
  'FEA': 'words',
  'NGRAM': '1-1',
  'PDS': 'T',
  'PSE': '-',
  'RN': '1dnf',
  'STEM': 'False',
  'UDS': 'He'},
 2: {'CLF': 'EMNB',
  'EM': 'False',
  'FEA': 'words',
  'NGRAM': '1-1',
  'PDS': 'TS',
  'PSE': '-',
  'RN': '1dnf',
  'STEM': 'False',
  'UDS': 'He'},
 3: {'CLF': 'EMNB',
  'EM': 'False',
  'FEA': 'words',
  'NGRAM': '1-1',
  'PDS': 'T',
  'PSE': '-',
  'RN': '1dnf',
  'STEM': 'False',
  'UDS': 'Te'}}

我需要根据一些特定的条件来过滤这个字典,所以我使用了内置的 filter 函数:

In [2]: filter(pred_func, eval_params.iteritems())
Out[2]: 
[(3,
  {'CLF': 'EMNB',
   'EM': 'False',
   'FEA': 'words',
   'NGRAM': '1-1',
   'PDS': 'T',
   'PSE': '-',
   'RN': '1dnf',
   'STEM': 'False',
   'UDS': 'Te'})]    

我的问题是,我不想把这些条件写死在 pred_func 里面,而是想动态地传递给 pred_func。现在的 pred_func 看起来是这样的:

def pred_func((i, d)):
    # I have to hard-code the parameters to filter by. I don't want that
    return d['PDS'] == 'T' and d['UDS'] = 'Te'

不过,我希望能有这样的写法:

def pred_func((i, d), predicates):
    vals = []
    # predicates would be a dictionary such as {'PDS':'T', 'UDS':'Te'}
    for param, val in predicates.iteritems():
        vals.append(d[param] == val)
    return all(vals)

但是,据我所知,我不能给条件函数传递额外的参数,那我该怎么优雅地实现我想要的功能呢?有什么好主意吗?

1 个回答

5

创建一个高阶函数 [维基百科],也就是说,这个函数会返回另一个函数。举个例子:

def pred_func(predicates):
    # Define a function inside `pred_func`. This function is a "closure" and 
    # will have access to the `predicates` parameter
    def func((i, d)):
        # You can simply pass a generator to `all`
        return all(d[param] == val for param, val in predicates.iteritems())

    # Return the function
    return func

pred_func 现在变成了一个函数,它会返回一个接受元组作为参数的函数(就像你原来的 pred_func 函数一样)。

然后你可以用以下方式调用这个函数:

filter(pred_func(predicates), eval_params.iteritems())

撰写回答