简体   繁体   English

字典映射中的表达式

[英]Expressions in a dictionary mapping

I have a series of conditionals of the form: 我有一系列形式的条件:

if ':' in particle:
    do something
elif 'eq' in particle:  
    do something else
elif 'lt' in particle:
    do another thing
elif 'le' in particle:
    etc.
elif 'gt' in particle:
    etc., etc.
elif 'ge' in particle:
    etc., etc., etc.
elif 'ne' in particle:
    more etc.

I want to implement this using a dictionary mapping pattern, but am having issues with the keys. 我想使用字典映射模式来实现此功能,但是键存在问题。

I tried this: 我尝试了这个:

def case_evaluator(particle):
    switcher = {
        ':' in particle: do something,
        'eq' in particle: do something else,
        'lt' in particle: do another thing,
        ...
    }
    return switcher.get(particle, "nothing")

But, I kept getting "nothing." 但是,我一直“一无所获”。 How can something give nothing? 东西什么也不能给?

This seems like it should be simple, but alas... 这看起来应该很简单,但是a ...

You're on the right track. 您走在正确的轨道上。 This is called function dispatch. 这称为函数分派。 It needs to look more like this: 它需要看起来像这样:

def case_evaluator(particle):
    switcher = {
        ':': do_something,
        'eq': do_something_else,
        'lt': do_another_thing,
        ...
    }
    return switcher.get(particle, lambda: "nothing")()

where do_something, etc are all functions that take no arguments. 其中do_something等都是不带参数的函数。 The lambda x: "nothing" is a lambda function that just always returns "nothing" -- it's the default function to be called if particle isn't found in switcher.keys() . lambda x: "nothing"是一个lambda函数,它总是返回“ nothing” -如果在switcher.keys()找不到particle则它是默认调用的函数。

You probably want to have a dictionary that maps characters to functions. 您可能想要一个将字符映射到函数的字典。

char_function_dict = {
    ':': colon_function,
    'eq': eq_function,
    'lt': lt_function
    # ...and so on...
}

Then, you can iterate over the key-value pairs in this dictionary. 然后,您可以遍历此字典中的键/值对。

def apply_function(particle):
    for char, function in char_function_dict.items():
        if char in particle:
            function()

However, note that this structure doesn't really use anything specific to dictionaries and doesn't preserve the order that the characters are checked. 但是,请注意,此结构实际上并没有使用任何特定于字典的内容,也没有保留检查字符的顺序。 It would perhaps be even more simple to use a list of 2-element tuples. 使用2元素元组列表可能会更加简单。

char_functions = [
    (':', colon_function),
    ('eq', eq_function),
    ('lt', lt_function)
    # ...and so on...
]

def apply_function(particle):
    for char, function in char_functions:
        if char in particle:
            function()
            break # so successive functions are not run

Setting up either of these structures to allow arguments and/or keyword arguments to be passed to the functions is easy: 设置这些结构之一以允许将参数和/或关键字参数传递给函数很容易:

def apply_function(particle, *args, **kwargs):
    for char, function in char_functions:
        if char in particle:
            function(*args, **kwargs)
            break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM