如何将一系列条件映射为字典中的键?

时间:2015-04-03 14:01:01

标签: python python-2.7 dictionary lambda

我知道你可以使用字典作为switch语句的替代,如下所示:

def printMessage(mystring):
    # Switch statement without a dictionary
    if mystring == "helloworld":
        print "say hello"
    elif mystring == "byeworld":
        print "say bye"
    elif mystring == "goodafternoonworld":
        print "good afternoon"


def printMessage(mystring):
    # Dictionary equivalent of a switch statement
    myDictionary = {"helloworld": "say hello",
                    "byeworld": "say bye",
                    "goodafternoonworld": "good afternoon"}
    print myDictionary[mystring]

但是,如果使用条件,除了返回true为false的等于(==)之外,这些不能很容易地映射,即:

if i > 0.5:
    print "greater than 0.5"
elif i == 5:
    print "it is equal to 5"
elif i > 5 and i < 6:
    print "somewhere between 5 and 6"

以上内容不能直接转换为字典键值对:

# this does not work
mydictionary  = { i > 0.5: "greater than 0.5" }

可以使用lambda,因为它是可散列的,但是从地图中获取结果字符串的唯一方法是将相同的lambda对象传递给字典,而不是在lambda的求值为真时:

x = lambda i: i > 0.5
mydictionary[x] = "greater than 0.5"
# you can get the string by doing this:
mydictionary[x]
# which doesnt result in the evaluation of x

# however a lambda is a hashable item in a dictionary
mydictionary = {lambda i: i > 0.5: "greater than 0.5"}

有没有人知道在lambda评估和返回值之间创建映射的技术或方法? (这可能类似于函数式语言中的模式匹配)

3 个答案:

答案 0 :(得分:16)

你的条件是连续性的;你想一个接一个地测试,而不是在这里将少量键映射到一个值。改变条件的顺序可能会改变结果;值5会导致样本中出现"greater than 0.5",而不是"it is equal to 5"

使用元组列表:

myconditions  = [
    (lambda i: i > 0.5, "greater than 0.5"),
    (lambda i: i == 5, "it is equal to 5"),
    (lambda i: i > 5 and i < 6, "somewhere between 5 and 6"),
]

之后你可以依次访问每一个,直到匹配:

for test, message in myconditions:
    if test(i):
        return message

重新排序测试将改变结果。

字典适用于您的第一个示例,因为对于由字典优化的多个静态值有一个简单的相等性测试,但这里没有这样简单的等式。

答案 1 :(得分:1)

您不能使用字典来映射任意条件,因为它们中的多个条件可能同时为真。相反,您需要按顺序评估每个,并在第一次遇到真实代码时执行相关代码。这里概述了正式实现类似甚至允许相当于default:案例的方法。

from collections import namedtuple

Case = namedtuple('Case', ['condition', 'code'])

cases = (Case('i > 0.5',
            """print 'greater than 0.5'"""),

         Case('i == 5',
            """print 'it is equal to 5'"""),

         Case('i > 5 and i < 6',
            """print 'somewhere between 5 and 6'"""))

def switch(cases, **namespace):
    for case in cases:
        if eval(case.condition, namespace):
            exec(case.code, namespace)
            break
    else:
        print 'default case'

switch(cases, i=5)

输出:

greater than 0.5

答案 2 :(得分:0)

没有直接相关,但我经常使用类似于下面示例的范例来用dictionaruy查找替换级联if。

def multipleifs(a=None,b=None,c=None,d=None,e=None):
    """ Func1 with cascaded if
    >>> multipleifs(10,20,30,40,50)
    160
    """

    x=10
    if a:
        x += 10
    if b:
        x += 20
    if c:
        x += 30
    if d:
        x += 40
    if e:
        x += 50

    return x

def dictif(a=None,b=None,c=None,d=None,e=None):
    """ Func2 with dictionary replacing multiple ifs
    >>> dictif(10,20,30,40,50)
    160
    """

    x, mydict = 10, dict(enumerate([10,20,30,40,50]))

    for count, item in enumerate([a,b,c,d,e]):
        if item: x += mydict.get(count,0)

    return x