如何映射一系列条件作为字典中的键? [英] How to map a series of conditions as keys in a dictionary?

查看:127
本文介绍了如何映射一系列条件作为字典中的键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know you can use a dictionary as an alternative to a switch statement such as the following:

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的错误,这些不容易被映射,即:

However if conditions are used, other than equality (==) which return true of false these cant be mapped as easily i.e.:

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"

以上不能直接转换为一个字典键值对是:

The above cannot be directly converted to a dictionary key-value pair as is:

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

可以使用lambda,因为它是可哈希的,但是将结果字符串从地图中取出的唯一方法是将相同的lambda对象传递到字典中,而不是当lambda的评估为true时:

A lambda can be used since it is hash-able but the only way to get the resulting string out of the map is by passing the same lambda object into the dictionary and not when the evaluation of the lambda is true:

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评估和返回值?
(这可能类似于功能语言中的模式匹配)

Does anyone know of a technique or method to create a mapping between a lambda evaluation and a return value? (this maybe similar to pattern matching in functional language)

推荐答案

您的条件本质上是顺序的;你想要一个接一个地测试,不要将一小部分键映射到一个值。改变条件的顺序可能会改变结果;值 5 导致样本中大于0.5,而不是等于5

Your conditions are sequential in nature; you want to test one after the other, not map a small number of keys to a value here. Changing the order of the conditions could alter the outcome; a value of 5 results in "greater than 0.5" in your sample, not "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"),
]

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

after which you can access each one in turn until one matches:

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

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

Re-ordering the tests will change the outcome.

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

A dictionary works for your first example because there is a simple equality test against multiple static values that is optimised by a dictionary, but there are no such simple equalities available here.

这篇关于如何映射一系列条件作为字典中的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆