python实现一句话全部反转,单词不反转

#encoding=utf-8
def recersal(content):
    x=0
    y=[]
    n3=[]
    word_content=re.findall(r"\w*[!.,?]*",content)
    n1=[i for i in range(len(word_content)) if word_content[i]!=""]
    n2=[len(i) for i in word_content if i !=""]
    z=n2[0]-1
    for i in range(len(n1)-1):
        n3.append(n1[i+1]-n1[i]-1)
        z+=n2[i+1]-1
    n3.append(len(content)-z-n1[-1]-1)
    for i in range(len(n1)):
        if n1[0]==0:
            y.append(content[x:x+n2[i]])
            y.append(content[x+n2[i]:x+n2[i]+n3[i]])
            x=x+n2[i]+n3[i]
    return y
if __name__=="__main__":
    import re
    import time
    time1=time.time()
    content="asd dssdf tyrt, asdf!!!"
    result=recersal(content)
    result.reverse()
    print content
    print "".join(result)
    print "_"*20
    print u"程序耗时%s" % str(time.time()-time1)
    print "_"*20
posted @ 2019-07-14 21:43  张特别  阅读(1133)  评论(0编辑  收藏  举报