如何解决Python中的索引超出范围错误?

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

How to solve index out of range error in python?

问题

  1. from sys import stdin
  2. def arrayRotateCheck(arr, n):
  3. i = 1
  4. while arr[i] > arr[i - 1] and i < n - 1:
  5. i += 1
  6. if i == n - 1:
  7. return 0
  8. else:
  9. return i
  10. # Taking Input Using Fast I/O
  11. def takeInput():
  12. n = int(stdin.readline().rstrip())
  13. if n == 0:
  14. return list(), 0
  15. arr = list(map(int, stdin.readline().rstrip().split()))
  16. return arr, n
  17. # main
  18. t = int(stdin.readline().rstrip())
  19. while t > 0:
  20. arr, n = takeInput()
  21. print(arrayRotateCheck(arr, n))
  22. t -= 1
英文:
  1. from sys import stdin
  2. def arrayRotateCheck(arr, n):
  3. i=1
  4. while arr[i]&gt;arr[i-1] and i&lt;n-1:
  5. i+=1
  6. if i==n-1:
  7. return 0
  8. else:
  9. return i
  10. #Taking Input Using Fast I/O
  11. def takeInput() :
  12. n = int(stdin.readline().rstrip())
  13. if n == 0:
  14. return list(), 0
  15. arr = list(map(int, stdin.readline().rstrip().split(&quot; &quot;)))
  16. return arr, n
  17. #main
  18. t = int(stdin.readline().rstrip())
  19. while t &gt; 0 :
  20. arr, n = takeInput()
  21. print(arrayRotateCheck(arr, n))
  22. t -= 1

This is a Python code finding array rotation index, the following line causing index out of range error. How to overcome this error.

  1. while arr[i]&gt;arr[i-1] and i&lt;n-1:

答案1

得分: 0

arrayRotateCheck函数中,当数组的长度为1时,程序的输出将是0。

以下是代码:

  1. def arrayRotateCheck(arr, n):
  2. i = 1
  3. if len(arr) == 1:
  4. return 0
  5. while arr[i] > arr[i - 1] and i < n - 1:
  6. i += 1
  7. if i == n - 1:
  8. return 0
  9. else:
  10. return i
英文:

you need to add a condition in arrayRotateCheck function, when the length of array is 1 then what will be the output of your program.

then here is code

  1. def arrayRotateCheck(arr, n):
  2. i = 1
  3. if len(arr) == 1:
  4. return 0
  5. while arr[i] &gt; arr[i - 1] and i &lt; n - 1:
  6. i += 1
  7. if i == n - 1:
  8. return 0
  9. else:
  10. return i

答案2

得分: 0

你的测试反过来了,所以它的第二部分没有像应该那样拯救你:

  1. while arr[i] > arr[i-1] and i < n-1:

在验证i是否小于n - 1之前,它会从arr[i]中读取并进行测试。如果你颠倒测试组件,使之在验证索引之前不从arr中读取(并且用户输入不会出现错误,n永远不会为零;如它所示,这种设计有点傻,因为n是用户所声称的输入数量,但arr的长度不必以任何方式与之相符),你就不会越界:

  1. while i < n-1 and arr[i] > arr[i-1]:

这会立即短路并在i大于或等于n - 1时不测试arr[i] > arr[i-1],从而退出循环。

另外,如果你从用户逐行接受输入,你不需要导入sys.stdin来访问它,你可以大大简化代码,用以下方式替换每个使用:

  1. stdin.readline().rstrip()

只需使用:

  1. input()

其中 input 从stdin中精确地读取一行,并为你删除换行符。我还建议调用.split()而不是.split(" ");后者如果用户在元素之间意外包含额外的空格,或者使用制表符,或者包含前导空格,将会表现不正常。前者将忽略所有前导和尾随空白,并在任何类型的空白的运行上分割,所以 1 2 3 会产生与 1 2 3 相同的结果。

英文:

Your test is backwards, so the second half of it doesn't save you the way it should:

  1. while arr[i]&gt;arr[i-1] and i&lt;n-1:

is reading from and testing arr[i] before it's actually validated that i is less than n - 1. If you flip the test components so it doesn't read from arr until it's validated the index (and the inputs from the user aren't lies and n is never zero; as is, it's kind of a silly design, because n is the user's asserted number of inputs, but arr's length doesn't have to agree with that in any way), you won't go out of bounds:

  1. while i &lt; n-1 and arr[i] &gt; arr[i-1]:

That short-circuits and doesn't even test arr[i] &gt; arr[i-1] when i is greater than or equal to n - 1, exiting the loop immediately.

As a side-note, if you're accepting input a line at a time from the user, you don't need an import to gain access to sys.stdin, and you can simplify the code significantly, replacing each use of:

  1. stdin.readline().rstrip()

with just:

  1. input()

where input reads precisely one line from stdin, and strips the newline for you. I'd also recommend calling .split() instead of .split(&quot; &quot;); the latter will misbehave if the user accidentally includes additional spaces between elements, or uses tabs, or includes a leading space. The former will ignore all leading and trailing whitespace, and split on runs of any type of whitespace, so 1 2 3 will produce the same result as 1 2 3.

huangapple
  • 本文由 发表于 2023年6月22日 01:24:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525774.html
匿名

发表评论

匿名网友

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

确定