为什么我在这里得到了索引超出范围的错误,请解释。

huangapple go评论65阅读模式
英文:

why am i getting index out of range error here please explain

问题

new_str = input()
for j in range(len(new_str)):
    for k in range(j+1, len(new_str)):
        if new_str[j] == new_str[k]:
            new_str = new_str[:k] + new_str[k+1:]
print(new_str)

这里我尝试删除字符串中重复的字符并打印出来,但是出现了"string index out of range"错误。我不明白为什么会出现这个错误。有人可以解释一下吗?
错误信息如下:
Traceback (most recent call last):
File "/tmp/submission/20230712/16/14/hackerrank-253aaa8879419e0d201489ccb06df6d8/code/Solution.py", line 17, in
merge_the_tools(string, k)
File "/tmp/submission/20230712/16/14/hackerrank-253aaa8879419e0d201489ccb06df6d8/code/Solution.py", line 7, in merge_the_tools
if new_str[j] == new_str[k]:
~~~~~~~^^^
IndexError: string index out of range


<details>
<summary>英文:</summary>

        new_str=input()
        for j in range(len(new_str)):
            for k in range(j+1,len(new_str)):
                if new_str[j]==new_str[k]:
                    new_str=new_str[:k]+new_str[k+1:]
        print(new_str)

Here I am trying to remove the repeated letters of a string and print it but I am getting the error  of string index out of range. I am not understanding why. Can anyone explain me why?
the error is
Traceback (most recent call last):
  File &quot;/tmp/submission/20230712/16/14/hackerrank-253aaa8879419e0d201489ccb06df6d8/code/Solution.py&quot;, line 17, in &lt;module&gt;
    merge_the_tools(string, k)
  File &quot;/tmp/submission/20230712/16/14/hackerrank-253aaa8879419e0d201489ccb06df6d8/code/Solution.py&quot;, line 7, in merge_the_tools
    if new_str[j]==new_str[k]:
                   ~~~~~~~^^^
IndexError: string index out of range

</details>


# 答案1
**得分**: 0

你在遍历循环时改变了字符串的长度,正如Taha的答案所述,这导致了`IndexError`。在这种情况下,最好使用`while`循环而不是`for`循环,它基于字符串的初始长度运行。

下面是更新后的代码。

```python
new_str = input()

left = 0
while left < len(new_str) - 1:
    right = left + 1

    while right < len(new_str):
        if new_str[left] == new_str[right]:
            new_str = new_str[:right] + new_str[right + 1:]

        right += 1

    left += 1

print(new_str)

在这里,leftright 分别代表你的迭代器 jk

输出(正面情况):

akka
ak

输出(负面情况):

ghij
ghij
英文:

You're changing the string length while iterating over the loop, as Taha's answer told, which is causing the IndexError. In this case, it's better to use a while loop rather than a for loop, which runs based on the initial length of the string.

Here's the updated code.

new_str=input()

left = 0
while left &lt; len(new_str)-1:
	right = left + 1

	while right &lt; len(new_str):
		if new_str[left] == new_str[right]:
			new_str = new_str[:right] + new_str[right+1:]

		right += 1

	left += 1

print(new_str)

left and right stand for your iterators j and k respectively here.

Output (Positive Case):

akka
ak

Output (Negative Case):

ghij
ghij

huangapple
  • 本文由 发表于 2023年7月13日 00:11:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定