简体   繁体   English

我得到一个 IndexError: list index out of range 错误

[英]I get a IndexError: list index out of range Error

I´m having some troubles on my code, that implements a function that verifies the lenght of a string and return that string with or without spaces.我的代码遇到了一些麻烦,它实现了一个 function 来验证字符串的长度并返回带有或不带有空格的字符串。

If String b >= 15 return b如果字符串 b >= 15 返回 b

If strinf < 15 return "number os spaces until len(b)2 + string b如果 strinf < 15 返回 "number os 空格直到 len(b)2 + string b

But I get a IndexError: list index out of range Error.但我得到一个 IndexError: list index out of range 错误。 I cannot figure out where or why.我不知道在哪里或为什么。

Traceback:追溯:

============= RESTART: C:\Users\pbo\Desktop\Trab_04.py ============= ============= 重启:C:\Users\pbo\Desktop\Trab_04.py =============

ascii_letters = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ Traceback (most recent call last): ascii_letters = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ Traceback(最近一次通话最后):

File "C:\Users\pbo\Desktop\Trab_04.py", line 30, in o2.append(a(o1[e]))文件“C:\Users\pbo\Desktop\Trab_04.py”,第 30 行,在 o2.append(a(o1[e]))

IndexError: list index out of range IndexError:列表索引超出范围

============= ============= ============= ============= ============= ==================================================== == ==============

Thanks a lot for any possible help.非常感谢任何可能的帮助。

  from random import seed
  from random import randint
  from random import choice
  from string import ascii_letters

  seed(930)

  def a(b):

    if(len(b)<15) :
        s = "";
        l = len(b)
        while(len(s) + len(b) < 15) :
            s += " "
        s += b
        return s
    else: 
        return b


 print("ascii_letters = " + ascii_letters)
 o1 = []
 for e in range(9622):
         k = randint(1, 25)
         s = ""
 for l in range(k):
         s = s + choice(ascii_letters)
 o1.append(s)
 o2 = []
 for e in range(9622):
         o2.append(a(o1[e]))
 print("000000000111111111122222222223333333333")
 print("123456789012345678901234567890123456789")
 for e in range(9):
         print(o1[e] + "|")
 print("000000000111111111122222222223333333333")
 print("123456789012345678901234567890123456789")
 for e in range(9):
         print(o2[e] + "|")


 x1 = ""
 x2 = "a"
 x3 = "abc"
 x4 = "abcdefghijklmnopqrstuvwxyz"
 print("000000000111111111122222222223333333333")
 print("123456789012345678901234567890123456789")
 print(x1 + "|")
 print(x2 + "|")
 print(x3 + "|")
 print(x4 + "|")
 print("000000000111111111122222222223333333333")
 print("123456789012345678901234567890123456789")
 print(a(x1) + "|")
 print(a(x2) + "|")
 print(a(x3) + "|")
 print(a(x4) + "|")

The problem is that your list o1 only has one element in it.问题是您的列表o1只有一个元素。 Snipping out all of the rest of the code, you do this:剪掉代码的所有 rest,你可以这样做:

s = "some string"
o1 = []
o2 = []

# now has one element
o1.append(s)

for i in range(9622):
    # when i > 0, IndexError will be raised
    state = o1[i]
    o2.append(a(state))   

This happens because you are iterating over a range that has no relationship to the size/contents of o1 .发生这种情况是因为您正在迭代与o1的大小/内容无关的范围。 To fix this, you might consider:要解决此问题,您可以考虑:

s = "some string"
o1 = []
o2 = []

o1.append(s)
for element in o1:
    o2.append(a(element))

The reason being is that now you are iterating over o1 directly, rather than assuming it will always have 9622 elements in it, which it doesn't in this case.原因是现在您正在直接迭代o1 ,而不是假设它总是有 9622 个元素,在这种情况下它没有。

Also, upon further inspection, it looks like your a function is just trying to pad a string with spaces out to 15 characters.此外,经过进一步检查,您a function 似乎只是试图用空格填充 15 个字符的字符串。 Fortunately, there's a builtin for this:幸运的是,有一个内置的:

x = "some string"

x.rjust(15)
'    some string'

Where rjust will left-insert spaces to create a string of the given length其中rjust将左插入空格以创建给定长度的字符串

It looks like your indentation is incorrect (see also What is Python Whitespace and how does it work? ).看起来您的缩进不正确(另请参阅什么是 Python 空白以及它是如何工作的? )。

If you change the following:如果您更改以下内容:

o1 = []
for e in range(9622):
    k = randint(1, 25)
    s = ""
for l in range(k):
    s = s + choice(ascii_letters)
o1.append(s)

to:至:

o1 = []
for e in range(9622):
    k = randint(1, 25)
    s = ""
    for l in range(k):
        s = s + choice(ascii_letters)
        o1.append(s)

Then, your code appears to work.然后,您的代码似乎可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按钮错误,但我得到IndexError:列表索引超出范围 - Button error, but I get IndexError: list index out of range 为什么会出现“ IndexError:列表索引超出范围”错误? - Why do I get a “IndexError: list index out of range” error? 当使用while循环创建“list”时,我得到此错误:IndexError:列表赋值索引超出范围 - I get this error when using a while loop to make a “list” IndexError: list assignment index out of range 为什么我得到一个 IndexError: list index out of range - Why do I get an IndexError: list index out of range 为什么我得到 IndexError: list index out of range - Why do I get IndexError: list index out of range 为什么我得到 IndexError: list assignment index out of range - why do i get the IndexError: list assignment index out of range 我收到了这个错误——IndexError: list index out of range - I got this error- IndexError: list index out of range 如何解决此错误:IndexError:列表索引超出范围 - How can I fix this error: IndexError: list index out of range 为什么我会收到此错误? IndexError:列表索引超出范围 - Why am I receiving this error? IndexError: list index out of range 当我运行此命令“cascPath = sys.argv[1]”时,我收到错误 IndexError: list index out of range - When i run this command " cascPath = sys.argv[1] " i get error IndexError: list index out of range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM