Discuss / Python / 用递归写的,显得有点复杂了,有没有更简洁的递归写法呀?

用递归写的,显得有点复杂了,有没有更简洁的递归写法呀?

Topic source

ksahgfiwhi

#1 Created at ... [Delete] [Delete and Lock User]

def trim(s):

    if s == '':

        return s

    elif s == ' ':

        s = ''

        return s

    else:

        if s[0] == ' ':

            s = s[1:]

            s = trim(s)

        if s == '':

            return s

        elif s[-1] == ' ' :

            s = s[:-1]

            s = trim(s)

        return s

寒刃

#2 Created at ... [Delete] [Delete and Lock User]

请问s=trim(s)是什么作用啊 为什么删掉就不行

lorierica

#3 Created at ... [Delete] [Delete and Lock User]

递归调用这个trim函数自行循环了

请问为什么

s=[0:-1]会报错呢


  • 1

Reply