我的 python 代码如何越界?

How is my python code going out of bound?

我一直在尝试将字符串 (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 条件来阻止它,在什么部分我的逻辑错了请解释...

首先这张支票是奇数:

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

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

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

并且在检查 i==len(string_value).

后不允许读取 string_value[i]

(我提醒你:"The break statement, like in C, breaks out of the innermost enclosing for or while loop.")

问题出在这里:

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

遍历字符串中的每个字符,然后检查下一个字符是否与当前字符相同。如果是,则添加一个其他将计数添加到临时字符串并将计数重置为 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