Python 减少if/elif的使用

Python 减少if/elif的使用,python,list,dictionary,if-statement,simplify,Python,List,Dictionary,If Statement,Simplify,有没有办法减少这个代码中的if和elif的数量 cod = ['menu'], ['dimension 1"'], ['dimension 2”'], ['dimension 3”'], ['dimension 4'], ['dimension 5'], ['dimension 6'], ['dimension 7'], ['dimension 8'], ['dimension 9'], ['dim

有没有办法减少这个代码中的if和elif的数量

cod =
    ['menu'],
    ['dimension 1"'],
    ['dimension 2”'],
    ['dimension 3”'],
    ['dimension 4'],
    ['dimension 5'],
    ['dimension 6'],
    ['dimension 7'],
    ['dimension 8'],
    ['dimension 9'],
    ['dimension 10']
]
return cod

    if choice == 1:
    quantify = float(input(('Quantify: ')))
    if 0 < quantify <= 6:
        print('± 0,1mm')
    elif 6 < quantify <= 30:
        print('± 0,2mm')
    elif 30 < quantify <= 120:
        print('± 0,3mm')
    elif 120 < quantify <= 400:
        print('± 0,5mm')
    elif 400 < quantify <= 1000:
        print('± 0,8mm')
    elif 1000 < quantify <= 2000:
        print('± 1,2mm')
    elif 2000 < quantify <= 4000:
        print('± 2mm')
    else:
        print('<<< Min = 0,5mm | Max = 4000mm >>>')
elif choice == 2:
    quantify = float(input(('Quantify: ')))
    if 0 < quantify <= 3:
        print('± 0,2mm')
    elif 3 < quantify <= 6:
        print('± 0,5mm')
    elif 6 < quantify:
        print('± 1mm')
    else:
        print('<<< Min = 0,5mm | Max = ∞ >>>')
*我必须改变一些东西使它可读,因为它是葡萄牙语和其他一些功能

谢谢,
soldcarvalho

替换包含严格相等比较的if…elif链非常简单。然而,当你有一个范围时,字典不是正确的解决方案。相反,您需要一个自定义数据结构,该结构将每个范围的开始和结束与if语句进行比较。然后,您可以使用for循环来循环范围列表中的所有范围。

您使用字典和列表的想法是正确的。这就是我如何简化代码,使其不使用一堆if-else语句的方法

from collections import namedtuple

tolerance = namedtuple( 'tolerance', ['lower_bound', 'upper_bound', 'tolerance'] )

tolerances = {
    '1': [
        tolerance(0, 6, '± 0,1mm'),
        tolerance(6, 30, '± 0,2mm'),
        tolerance(30, 120, '± 0,3mm'),
        tolerance(120, 400, '± 0,5mm'),
        tolerance(400, 1000, '± 0,8mm'),
        tolerance(1000, 2000, '± 1,2mm'),
        tolerance(2000, 4000, '± 2mm')
    ],
    '2': [
        tolerance(0, 3, '± 0,2mm'),
        tolerance(3, 6, '± 0,5mm'),
        tolerance(6, 'inf', '± 1mm'),
    ]
}

def get_tolerance(quantify, choice):
    for t in tolerances[choice]:
        if t.lower_bound <= quantify <= t.upper_bound:
            return t.tolerance

    # if we didn't find a matching tolerance, raise an error
    min_tolerance = min( t.lower_bound for t in tolerances[choice] )
    max_tolerance = max( t.lower_bound for t in tolerances[choice] )
    raise ValueError(f'<<< Min = {min_tolerance} | Max = {max_tolerance} >>>')


choice = input('Choice: ')
while choice not in tolerances:
    print('Invalid choice. Choice must be one of the following:', ', '.join(tolerances.keys()))
    choice = input('Choice: ')

quantify = float(input('Quantify: '))
print('Tolerance is:', get_tolerance(quantify, choice))

我选择使用名为a的东西来存储公差信息,而不是列表。如果您从未听说过命名元组,那么它们的工作方式是指定一些字段,例如下限、上限、容差,然后返回在本例中可以用于创建命名元组的容差。当我们使用一个公差来创建一个命名元组时,例如t=tolerance 0,6,“±0,1mm”,那么我们就可以使用t.lower_bound来获得下限,t.upper_bound来获得上限,t.tolerance。当您需要存储固定数量的项时,使用这样的命名元组而不是列表,可以使代码更易于阅读。

这里有两种方法。注意到其他人也发布了类似的内容:

""" SO QA """

import bisect


class Tolerance:
    def __init__(self, lower_limit, text):
        self._lower_limit = lower_limit
        self._text = text

    def __eq__(self, other):
        return self.lower_limit == other.lower_limit

    def __lt__(self, other):
        return self.lower_limit < other.lower_limit

    def __gt__(self, other):
        return self.lower_limit > other.lower_limit

    def __str__(self):
        return '{:4d} {}'.format(self.lower_limit, self.text)

    @property
    def lower_limit(self):
        return self._lower_limit

    @property
    def text(self):
        return self._text

    @staticmethod
    def get(tolerance_list, value):
        if isinstance(value, int):
            value = Tolerance(value, "")
        if isinstance(value, Tolerance):
            idx = bisect.bisect_right(tolerance_list, value)
            if idx:
                return tolerance_list[idx-1]
            raise ValueError('value {} is out of range'.format(value))
        raise TypeError('unexpected type: {}'.format(type(value)))


_TOLERANCES = [
    Tolerance(   0, '± 0,1mm'),
    Tolerance(   6, '± 0,2mm'),
    Tolerance(  30, '± 0,3mm'),
    Tolerance( 120, '± 0,5mm'),
    Tolerance( 400, '± 0,8mm'),
    Tolerance(1000, '± 1,2mm'),
    Tolerance(2000, '± 2,0mm'),
    Tolerance(4000, 'over range'),
]


def get_tolerance_using_dict(value):
    """
    Based on the value, get the tolerance entry.
    :param value: The integer value that is within the expected range.
    :returns:     A entry dict {lower_limit: value, text: ""}.
    """
    tolerances = [
        {'lower_limit':    0, 'text': '± 0,1mm'},
        {'lower_limit':    6, 'text': '± 0,2mm'},
        {'lower_limit':   30, 'text': '± 0,3mm'},
        {'lower_limit':  120, 'text': '± 0,5mm'},
        {'lower_limit':  400, 'text': '± 0,8mm'},
        {'lower_limit': 1000, 'text': '± 1,2mm'},
        {'lower_limit': 2000, 'text': '± 2,0mm'},
        {'lower_limit': 4000, 'text': 'over range'},
    ]

    prev_entry = None
    for entry in tolerances:
        if entry['lower_limit'] <= value:
            prev_entry = entry
        else:
            return prev_entry
    return tolerances[len(tolerances) - 1]
    # Perhaps a better way to handle the range error:
    # raise ValueError('value is out of range'.format(value))


def main():
    """ Test the stuff """
    values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 29, 30, 100, 119, 120, 121,
              1999, 2000, 2001, 3999, 4000, 4001]

    print('using class:')
    for value in values:
        print('{:3} : {}'.format(value, Tolerance.get(_TOLERANCES, value)))

    print('using dict:')
    for value in values:
        print('{:3} : {}'.format(value, get_tolerance_using_dict(value)))



if __name__ == "__main__":
    main()

多谢。我会尝试这些代码,但我不能理解其中的大部分,因为我还没有学会如何使用类。所以,我在理解代码方面有点困难。。但是我会把它保留到有一天我理解它时可以使用它。再次感谢纳特索兹。