1

我是 Python 的新手,所以也许我要求一些非常简单的东西,但我无法以 Python 的方式思考问题。

我有一个压缩字符串。这个想法是,如果一个角色重复 4-15 次,我会做出这样的改变:

'0000' ---> '0|4'

如果超过 15 次,我使用斜线和两位数字来表示数量(使用十六进制值):

'00...(16 times)..0' ---> '0/10'

所以,习惯了其他语言,我的方法如下:

def uncompress(line):
    verticalBarIndex = line.index('|')
    while verticalBarIndex!=-1:
        repeatedChar = line[verticalBarIndex-1:verticalBarIndex]
        timesRepeated = int(line[verticalBarIndex+1:verticalBarIndex+2], 16)
        uncompressedChars = [repeatedChar]
        for i in range(timesRepeated):
            uncompressedChars.append(repeatedChar)
        uncompressedString = uncompressedChars.join()
        line = line[:verticalBarIndex-1] + uncompressedString + line[verticalBarIndex+2:]
        verticalBarIndex = line.index('|') #next one

    slashIndex = line.index('/')
    while slashIndex!=-1:
        repeatedChar = line[slashIndex-1:slashIndex]
        timesRepeated = int(line[slashIndex+1:verticalBarIndex+3], 16)
        uncompressedChars = [repeatedChar]
        for i in range(timesRepeated):
            uncompressedChars.append(repeatedChar)
        uncompressedString = uncompressedChars.join()
        line = line[:slashIndex-1] + uncompressedString + line[slashIndex+3:]
        slashIndex = line.index('/') #next one
    return line

我知道这是错误的,因为字符串在 Python 中是不可变的,并且我一直在更改行内容,直到没有 '|' 或“/”存在。

我知道 UserString 存在,但我想有一种更简单、更 Pythonish 的方式来做这件事,这将是很好的学习。

有什么帮助吗?

4

3 回答 3

2

使您的代码与示例字符串一起运行所需的更改:

更改.index().find().index()如果未找到子字符串,则引发异常,.find()返回 -1。

更改uncompressedChars.join()''.join(uncompressedChars)

更改timesRepeated = int(line[slashIndex+1:verticalBarIndex+3], 16)timesRepeated = int(line[slashIndex+1:slashIndex+3], 16)

设置uncompressedChars = []为开头,而不是uncompressedChars = [repeatedChar].

这应该让它正常工作。有很多地方需要整理和优化代码,但这很有效。

于 2012-10-31T12:16:38.210 回答
0

我见过的最常见的模式是使用字符列表。列表是可变的,并且可以按照您上面的描述工作。

从字符串创建列表

mystring = 'Hello'
mylist = list(mystring)

从列表创建字符串

mystring = ''.join(mylist)
于 2012-10-31T12:12:44.417 回答
0

您应该建立一个子字符串列表,并在最后加入它们:

def uncompress(line):
  # No error checking, sorry. Will crash with empty strings.
  result = []
  chars = iter(line)
  prevchar = chars.next()   # this is the previous character
  while True:
    try:
      curchar = chars.next()  # and this is the current character
      if curchar == '|':
        # current character is a pipe.
        # Previous character is the character to repeat
        # Get next character, the number of repeats
        curchar = chars.next()
        result.append(prevchar * int(curchar, 16))
      elif curchar == '/':
        # current character is a slash.
        # Previous character is the character to repeat
        # Get two next characters, the number of repeats
        curchar = chars.next()
        nextchar = chars.next()
        result.append(prevchar * int(curchar + nextchar, 16))
      else:
        # No need to repeat the previous character, append it to result.
        result.append(curchar)
      prevchar = curchar
    except StopIteration:
      # No more characters. Append the last one to result.
      result.append(curchar)
      break
  return ''.join(result)
于 2012-10-31T12:19:47.153 回答