美文网首页
python逆置单词的三种方式(字符串同理)

python逆置单词的三种方式(字符串同理)

作者: a嘿嘿_ccee | 来源:发表于2020-06-02 22:04 被阅读0次
    str = "this is a good day"
    list = str.split()
    print(list)
    temp = []
    flag = 0
    while len(list)>0:
        temp.append(list.pop())
    print(temp)
    
    
    
    str = 'this is a good day'
    list = str.split()
    lo = 0
    hi = len(list)-1
    while lo<hi:
        temp = list[lo]
        list[lo] = list[hi]
        list[hi] = temp
        lo+=1
        hi-=1
    print(list)
    
    str = 'this is a good day'
    list = str.split()
    def reserve(list, lo, hi):
        if lo<hi:
            temp = list[lo]
            list[lo] = list[hi]
            list[hi] = temp
            reserve(list, lo+1, hi-1)
    reserve(list, 0, len(list)-1)
    print(list)
    

    相关文章

      网友评论

          本文标题:python逆置单词的三种方式(字符串同理)

          本文链接:https://www.haomeiwen.com/subject/gexmzhtx.html