regex
  • python-3.x
  • 2017-08-30 106 views -2 likes 
    -2
    import re 
    for test in range(int(input())): 
        a = input()     # input a string 
        n = a.replace("=", "")  # if string contains '='then remove it 
        gg = re.findall(r ">+", n) # count > 
        l1 = len(max(gg, key = len)) # count consecutive > 
        hh = re.findall(r "<+", n) # count < 
        l2 = len(max(hh, key = len)) # count consecutive < 
        print(max(l1, l2) + 1)  # print max of two + 1 
    

    输入是:
    < < <
    < > <
    < =>
    < = <
    我遇到错误,如果我运行上述code.I上阅读SO只有语法仍然我得到错误:re.findall()的代码中有什么错误?

    Traceback (most recent call last):<br/> File "/home/fea0d5e04ac92cb3a1e4f041940f2dfc.py", line 8, in <module><br/> 
    l2=len(max(hh, key=len))<br/> ValueError: max() arg is an empty sequence 
    
    +0

    请包括你得到的错误。当我运行代码时,它工作正常。 – user3080953

    +0

    @ user3080953编辑它 – user8539458

    +0

    您的意见是什么? –

    回答

    0

    max在空序列上失败。

    的Python> = 3.4

    max有一个可选的默认参数:

    l1 = len(max(gg, key = len, default=0)) # count consecutive >

    的Python < 3.4

    添加保护检查列表是空的

    if len(hh) > 0: 
        l2 = len(max(hh, key = len)) # count consecutive < 
    else: 
        l2 = 0 
    
    相关问题