• 01——字符串反转


    第一种:使用分片,使用字符串、列表

    result = s[::-1]

    第二种:使用列表的reverse方法,适用于列表

    li =['b', 'c', 'f', 4, 5, 6]
    a = list(reversed(li))
    print (a)
    输出:
    [6, 5, 4, 'f', 'c', 'b']

    第三种:使用reduce

    result = reduce(lambda x,y:y+x,s)                    #不认识,先记着

    第四种:使用递归函数

    def func(s):                        #此处使用递归,不断把第一个字符,通过拼接的方式加到分片字符串后面,而分片字符串是将函数实参的第一个字符丢弃
        if len(s) <1:
            return s
        return func(s[1:])+s[0]
    result = func(s)

    第五种:使用栈

    def func(s):
        l = list(s) #模拟全部入栈
        result = ""
        while len(l)>0:
            result += l.pop() #模拟出栈
        return result
    result = func(s)

    第六种:for循环

    def func(s):
        result = ""
        max_index = len(s)-1
        for index,value in enumerate(s):
            result += s[max_index-index]
        return result
    result = func(s)
  • 相关阅读:
    url 编码与解码
    调硬件开门
    JsBridge 开灯关灯
    滚动条样式
    uni-app 组件传值及插槽
    Eapp 调接口及跳转
    uni-app 的基础格式
    Eapp 几个弹框
    flexible 移动端适配
    mongodb恢复备份
  • 原文地址:https://www.cnblogs.com/mailong/p/12003258.html
Copyright © 2020-2023  润新知