根据不同的标准规则检查列表

时间:2012-02-10 14:05:50

标签: python list list-comprehension

给定一个项目列表,我想创建一个函数来检查它是否符合某个标准并返回true或false。

以下是标准和预期输出的示例:

    #the to check against with criteria[1]
    langList = ['spanish','english','russian','persian']

    criteria = ['any'] #matches any language
    result: match

    #langList does not contain one or all
    criteria = ['any-but-or',[english,chinese]] 
    result: no match

    #langList does not contain all
    criteria = ['any-but-and',[english,chinese]] 
    result: match

    #langList contains one or all
    criteria = ['any-with-or',[english,chinese]] 
    result: match

    #langList contains all
    criteria = ['any-with-and',[english,chinese]] 
    result: no match

    #langList contains one or all and no others
    criteria = ['only-with-or',[english,chinese]] 
    result: no match

    #langList contains all and no others
    criteria = ['only-with-and',[english,chinese]] 
    result: no match

最好的方法是什么? 我的想法类似于下面但是我还没有掌握列表理解,但我认为这是关键。

    def check_criteria_match(criteria, languageList):

        rule = criteria[0]
        criteriaLanguages = criteria[1]
        match = True

        if rule != 'any':
            continue
        elif rule = 'any-but-or' #check languageList:
            match = False; break

        #[...]

        return match

编辑: 基于Praveen Gollakota的answer,这是最后的功能,不是很漂亮:)

def check_rule(rule, o, p):
    # o = original, p = pattern
    if rule == 'any':
        return True
    elif rule == 'any-but-or':
        return not bool(set(p)-set(o))
    elif rule == 'any-but-and':
        return len(set(p)-set(o)) != 0
    elif rule == 'any-with-or':
        return len(set(p)-set(o)) <= 1
    elif rule == 'any-with-and':
        return len(set(p)-set(o)) == 0
    elif rule == 'only-with-or':
        return len(set(o)-set(p)) <= 1
    elif rule == 'only-with-and':
        return len(set(o)-set(p)) == 0

2 个答案:

答案 0 :(得分:3)

您希望进行成员资格测试,sets是适当的数据结构,而set操作效果很好。

您可以使用sets和lambda函数来构建基于规则的调度程序。

>>> rule_checker = {'any': lambda o, p: True, 
...                 'any-but-or': lambda o, p: not bool(set(p) - set(o)),
...                 'any-but-and': lambda o, p: len(set(p) - set(o)) != 0,}

>>> lang_list = ['spanish','english','russian','persian']
>>> rule_checker['any-but-and'](lang_list, ['english','chinese'])
True
>>> rule_checker['any-but-or'](lang_list, ['english','chinese'])
False

当然,你需要在那里添加其余的规则。

编辑:你也可以在这样的函数中编写规则。

>>> def check_rule(rule, o, p):
...     # o = original, p = pattern
...     if rule == 'any':
...         return True
...     elif rule == 'any-but-or':
...         return not bool(set(p)-set(o))
...     elif rule == 'any-but-and':
...         return len(set(p)-set(o)) != 0
... 
>>> check_rule('any-but-and', lang_list, ['english','chinese'])
True

答案 1 :(得分:2)

您可以使用asq之类的库:

  

asq 是一个基于LINQ灵感的Python API的简单实现   操作Python迭代,包括并行版本   根据Python标准库多处理实现   模块。 API运动功能与对象的LINQ等效,100%   声明测试覆盖率和comprehensive documentation

实施例

>>> from asq.initiators import query
>>> langList = ['spanish', 'english', 'russian', 'persian']

# langlist contains 'russian'
>>> query(langList).contains('russian')
True

# langlist contains 'english' or 'italian'
>>> query(langList).any(lambda x : x == 'english' or x == 'italian')
True

# langlist contains 'english' and 'russian'
>>> not query(('english', 'russian')).difference(langList).any()
True