对字符串进行多次修改:如何,在Python中是不可变的?

2024-06-16 12:52:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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风格的方法,这将是一个很好的学习方法。你知道吗

有什么帮助吗?你知道吗


Tags: 方法字符串inforindexlinerangeint
3条回答

我见过的最常见的模式是使用字符列表。列表是可变的,正如您上面描述的那样工作。你知道吗

从字符串创建列表

mystring = 'Hello'
mylist = list(mystring)

从列表创建字符串

mystring = ''.join(mylist)

您应该在运行时构建一个子字符串列表,并在末尾加入它们:

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)

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

.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]。你知道吗

这应该能让它正常工作。有很多地方的代码可以整理和优化,但这是可行的。你知道吗

相关问题 更多 >