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

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

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

问题

  1. new_str = input()
  2. for j in range(len(new_str)):
  3. for k in range(j+1, len(new_str)):
  4. if new_str[j] == new_str[k]:
  5. new_str = new_str[:k] + new_str[k+1:]
  6. 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

  1. <details>
  2. <summary>英文:</summary>
  3. new_str=input()
  4. for j in range(len(new_str)):
  5. for k in range(j+1,len(new_str)):
  6. if new_str[j]==new_str[k]:
  7. new_str=new_str[:k]+new_str[k+1:]
  8. print(new_str)
  9. 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?
  10. the error is
  11. Traceback (most recent call last):
  12. File &quot;/tmp/submission/20230712/16/14/hackerrank-253aaa8879419e0d201489ccb06df6d8/code/Solution.py&quot;, line 17, in &lt;module&gt;
  13. merge_the_tools(string, k)
  14. File &quot;/tmp/submission/20230712/16/14/hackerrank-253aaa8879419e0d201489ccb06df6d8/code/Solution.py&quot;, line 7, in merge_the_tools
  15. if new_str[j]==new_str[k]:
  16. ~~~~~~~^^^
  17. IndexError: string index out of range
  18. </details>
  19. # 答案1
  20. **得分**: 0
  21. 你在遍历循环时改变了字符串的长度,正如Taha的答案所述,这导致了`IndexError`。在这种情况下,最好使用`while`循环而不是`for`循环,它基于字符串的初始长度运行。
  22. 下面是更新后的代码。
  23. ```python
  24. new_str = input()
  25. left = 0
  26. while left < len(new_str) - 1:
  27. right = left + 1
  28. while right < len(new_str):
  29. if new_str[left] == new_str[right]:
  30. new_str = new_str[:right] + new_str[right + 1:]
  31. right += 1
  32. left += 1
  33. print(new_str)

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

输出(正面情况):

  1. akka
  2. ak

输出(负面情况):

  1. ghij
  2. 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.

  1. new_str=input()
  2. left = 0
  3. while left &lt; len(new_str)-1:
  4. right = left + 1
  5. while right &lt; len(new_str):
  6. if new_str[left] == new_str[right]:
  7. new_str = new_str[:right] + new_str[right+1:]
  8. right += 1
  9. left += 1
  10. print(new_str)

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

Output (Positive Case):

  1. akka
  2. ak

Output (Negative Case):

  1. ghij
  2. 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:

确定