简化 python 3.6 中的 if 语句

simplify if statement in python 3.6

我有以下代码,它工作正常并给我预期的结果,但我知道有一种方法可以简化它,但我不知道最好的方法是 3/4行而不是 20 多行左右。 Python 专家需要您的建议,我如何简化以下代码。

for ele in hng_row:
    if not ele.isdigit():
        if not ele.isalpha():
            if not ele.isalnum():
                if ele.endswith('.00'):
                    get_index = hng_row.index(ele)
                    ele = ele[:-1]
                    hng_row[get_index] = ele
                if ele.startswith('0.') and ele.endswith('0') and ele != '0.0':
                    get_index = hng_row.index(ele)
                    ele = ele[:-1]
                    hng_row[get_index] = ele
                if (ele[-2] != '0' ) and (ele[-2] != '.') and (ele[-1] == '0'):
                    get_index = hng_row.index(ele)
                    ele = ele[:-1]
                    hng_row[get_index] = ele

前几个 if 语句可以放在一行中。

  for ele in hng_row:
        if not ele.isdigit() and not ele.isalpha() and not ele.isalnum():
            if ele.endswith('.00'):
                get_index = hng_row.index(ele)
                ele = ele[:-1]
                hng_row[get_index] = ele
            if ele.startswith('0.') and ele.endswith('0') and ele != '0.0':
                get_index = hng_row.index(ele)
                ele = ele[:-1]
                hng_row[get_index] = ele
            if (ele[-2] != '0' ) and (ele[-2] != '.') and (ele[-1] == '0'):
                get_index = hng_row.index(ele)
                ele = ele[:-1]
                hng_row[get_index] = ele

第一步:结合条件(+pylint)

for ele in hng_row:
    if not ele.isdigit() and not ele.isalpha() and not ele.isalnum():
        if ele.endswith('.00'):
            get_index = hng_row.index(ele)
            ele = ele[:-1]
            hng_row[get_index] = ele
        if ele.startswith('0.') and ele.endswith('0') and ele != '0.0':
            get_index = hng_row.index(ele)
            ele = ele[:-1]
            hng_row[get_index] = ele
        if (ele[-2] != '0') and (ele[-2] != '.') and (ele[-1] == '0'):
            get_index = hng_row.index(ele)
            ele = ele[:-1]
            hng_row[get_index] = ele

第二步:重构 if-blocks

for ele in hng_row:
    if not ele.isdigit() and not ele.isalpha() and not ele.isalnum():
        if (ele.endswith('.00')
                or ele.startswith('0.') and ele.endswith('0') and ele != '0.0'
                or (ele[-2] != '0') and (ele[-2] != '.') and (ele[-1] == '0')):
            get_index = hng_row.index(ele)
            ele = ele[:-1]
            hng_row[get_index] = ele

这可以进一步简化(或者,好吧,可以说 "shortened")。首先,请注意检查 not isdigitnot isalphanot isalnum 是多余的,您只需检查 not isalnum。其次,您可以使用 regular expression 来检查数字的格式,将您的三个条件与 | 结合起来。此外,您可以 enumerate 项目而不是获取 index.

for index, ele in enumerate(hng_row):
    if not ele.isalnum() and re.match(r"^.*\.00|0\..+0|.*[^0.]0$", ele):
        hng_row[index] = ele[:-1]

这里的正则表达式是 ^.*\.00|0\..+0|.*[^0.]0$^ 标记字符串的开始,$ 结束,| 是析取,即字符串必须匹配 .*\.00(后面跟着 .00)或 0\..+00.,然后是 0)或 .*[^0.]0(后面既没有 0 也没有 .,然后 0).

您也可以用列表理解替换循环:

>>> hng_row =  ['1531402200', 'primary', '2', '2100.00', '1.03', '1.05', '1.01', '2', '151'] 
>>> p = r"^.*\.00|0\..+0|.*[^0.]0$"
>>> [ele[:-1] if re.match(p, ele) else ele for ele in lst]
['1531402200', 'primary', '2', '2100.0', '1.03', '1.05', '1.01', '2', '151']