当前位置:天才代写 > tutorial > Python教程 > python 基本字符串压缩

python 基本字符串压缩

2018-05-18 08:00 星期五 所属: Python教程 浏览:436

python">class Zipper:
    def zipString(self, iniString):
        nlen = len(iniString)
        rest =[]
        i = 0
        endflag = 0
        while i < (nlen-1):
            for j in range(i+1, nlen):
                if iniString[i] == iniString[j]:
                    continue
                else:
                     rest.append((iniString[i], i, j))
                     endflag = j
                     break
            i = j

        rest.append((iniString[endflag], endflag, nlen))
        resStr = ""
        for item in rest:
            resStr += (item[0] + str(item[2]-item[1]))

        if len(resStr) >= nlen:
            return iniString
        else:
            return resStr
a = Zipper()
print a.zipString('aabcccccaaa')

b = Zipper()
print b.zipString('welcometonowcoderrrrr')

c = Zipper()
bstr= c.zipString('qwertyuioplkjhgfdsAzxcvbNM')
print type(bstr)

标签:python

 

    关键字:

天才代写-代写联系方式