regex
  • python-3.x
  • 2017-08-30 46 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 
    
    相關問題