【问题标题】:simplify if statement in python 3.6简化python 3.6中的if语句
【发布时间】:2018-07-12 13:03:10
【问题描述】:

我有以下代码,它运行良好并给了我预期的结果,但我知道有一种方法可以简化它,我不知道最好的方法是让以下代码改为 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

【问题讨论】:

  • 如果您解释一下代码的作用会有所帮助。
  • 我投票决定将此问题作为题外话结束,因为它应该发布在codereview.stackexchange.com
  • 实际输入和预期输出是多少?考虑另一种方法可能比尝试修复这个方法更容易。
  • 如果您的代码正常工作,这个问题可能更适合codereview.stackexchange.com
  • 哦,谢谢,我不知道 codereview 的事情。将删除此问题并将其发布在此处

标签: python if-statement simplify


【解决方案1】:

这可以进一步简化(或者,好吧,让我们说“缩短”)。首先,请注意检查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']

【讨论】:

  • 出现以下错误 - AttributeError: module 're' has no attribute 'matches'
  • @PrateekGupta 哎呀,应该是re.match(我没有真正测试这个,因为你没有提供测试用例)
  • 谢谢我更正了另一件事我有以下列表并且代码不会继续更改任何值 ['1531402200', 'primary', '2', '2100.00', '1.03 ', '1.05', '1.01', '2', '151']
  • 实际上应该将值从 2100.00 更改为 2100.0
  • @PrateekGupta 奇怪,它对我有用。查看我的更新。
【解决方案2】:

第一步:组合条件(+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

【讨论】:

  • 第三:再次组合条件,第四:提取条件作为辅助函数以提高可维护性
  • 另外,isdigitisalphaisalnum 是多余的。
  • 你能举个例子吗
【解决方案3】:

前几个 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

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    相关资源
    最近更新 更多