【问题标题】:How is my python code going out of bound?我的 python 代码是如何越界的?
【发布时间】:2020-06-25 16:59:38
【问题描述】:

我一直在尝试将字符串 (ex: aabbbacc) 编码为 a2b3a1c2 之类的东西 这是我尝试过的代码:

string_value = "aabbbacc"
temp_string = ""
for i in range(0, len(string_value)):
    if i != len(string_value) or i > len(string_value):
        temp_count = 1
        while string_value[i] == string_value[i+1]:
            temp_count += 1
            i += 1
        temp_string += string_value[i] + str(temp_count)
print(temp_string)

问题是即使我添加了if 条件来阻止越界发生,我仍然收到错误

Traceback (most recent call last):
  File "C:run_length_encoding.py", line 6, in <module>
    while string_value[i] == string_value[i+1]:
IndexError: string index out of range

我也试过

string_value = "aabbbacc"
temp_string = ""
for i in range(0, len(string_value)):
    count = 1
    while string_value[i] == string_value[i+1]:
        count += 1
        i += 1
        if i == len(string_value):
            break
    temp_string += string_value[i]+ str(count)
print(temp_string)

现在,我知道可能有更好的方法来解决这个问题,但我试图理解为什么我得到了越界异常,即使我有一个 if 条件来阻止它,在什么部分逻辑我错了请解释...

【问题讨论】:

  • 你的问题在这里string_value[i] == string_value[i+1]:
  • 想一想当i 是字符串的最后一个值并且您执行i+1 时会发生什么。
  • 更准确地说,问题在于i != len(string_value) or i &gt; len(string_value) 始终是True。这并不能防止 i 达到您想要的字符串的最后一个索引。

标签: python python-3.x python-2.7 indexoutofboundsexception stringindexoutofbounds


【解决方案1】:

遍历字符串中的每个字符,然后检查下一个字符是否与当前字符相同。如果是,则添加另一个将计数添加到临时字符串并将计数重置为 1。

string_value = "aabbbacc"
temp_string = ""
count = 1
for i in range(len(string_value)-1):
    if string_value[i] == string_value[i+1]:
        count += 1
    else:
        temp_string += string_value[i]+ str(count)
        count = 1

#add the last char count
temp_string += string_value[i+1]+ str(count)

print(temp_string)
Out:  a2b3a1c2

【讨论】:

    【解决方案2】:

    问题出在这里:

    for i in range(0, len(string_value)): # if i is the last index of the string
        count = 1
        while string_value[i] == string_value[i+1]: # i+1 is now out of bounds
    

    避免越界的最简单方法是根本不索引字符串:

    def encode(s):
        if s == '':   # handle empty string
            return s
        current = s[0]  # start with first character (won't fail since we checked for empty)
        count = 1
        temp = ''
        for c in s[1:]:  # iterate through remaining characters (string slicing won't fail)
            if current == c:
                count += 1
            else: # character changed, output count and reset current character and count
                temp += f'{current}{count}'
                current = c
                count = 1
        temp += f'{current}{count}'  # output last count accumulated
        return temp
    
    print(encode('aabbbacc'))
    print(encode(''))
    print(encode('a'))
    print(encode('abc'))
    print(encode('abb'))
    

    输出:

    a2b3a1c2
    
    a1
    a1b1c1
    a1b2
    

    【讨论】:

      【解决方案3】:

      首先这个检查很奇怪:

      if i != len(string_value) or i > len(string_value):
      

      其次,您检查 i 但读取 i+1 的值,并且可能是下一个...

      所以我的建议是将条件放在你的 while 中。

      并且在您检查了 i==len(string_value) 之后不允许读取 string_value[i]。

      (我提醒你:“break 语句,就像在 C 中一样,跳出最里面的 for 或 while 循环。”)

      【讨论】:

        猜你喜欢
        • 2013-01-17
        • 1970-01-01
        • 2021-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多