我遇到一个错误:在处理python项目时,列表索引超出范围。

我遇到一个错误:在处理python项目时,列表索引超出范围。,python,Python,我编写这段代码是为了以网格形式显示列表的内容。 它适用于字母表列表。 但当我尝试使用随机生成的列表运行它时,它会给出一个列表索引超出范围的错误 以下是完整的代码: 随机输入 #barebones 2d shell grid generator ''' Following list is a place holder you can add any list data to show in a grid pattern with this tool ''' lis = ['a','b','c

我编写这段代码是为了以网格形式显示列表的内容。 它适用于字母表列表。 但当我尝试使用随机生成的列表运行它时,它会给出一个列表索引超出范围的错误

以下是完整的代码: 随机输入

#barebones 2d shell grid generator



'''
Following list is a place holder
you can add any list data to show in a grid pattern with this tool
'''
lis = ['a','b','c','d','e','f','g','h','j','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

newLis = []
#generates random list
def lisGen():
    length = 20 # random.randint(10,20)
    for i in range(length):
        value = random.randint(1,9)
        newLis.append(str(value))

lisGen()

askRow = input('Enter number of rows :')
askColumns = input('Enter number of columns :')



def gridGen(row,column):
    j=0

    cnt = int(row)
    while (cnt>0):

        for i in range(int(column)):
            print(' '+'-',end='')
        print('\n',end='')
#this is the output content loop
        for i in range(int(column)):
            if j<len(lis):
                print('|'+newLis[j],end='')
                j += 1
            else:
                print('|'+' ',end='')

        print('|',end='')
        print('\n',end='')
        cnt -= 1

    for i in range(int(column)):
        print(' '+'-',end='')
    print('\n',end='')




gridGen(askRow,askColumns)
使用随机生成的列表(newLis)时的错误输出:

输入行数:7
输入列数:7
- - - - - - -
|9|2|1|4|7|5|4|
- - - - - - -
|9|7|7|3|2|1|3|
- - - - - - -
|7 | 5 | 4 | 1 | 2 | 3回溯(最后一次最近通话):
文件“D:\01 myware\python\2d shell graphics\gridGen.py”,第56行,在
gridGen(askRow、askColumns)
gridGen中第40行的文件“D:\01 myware\python\2d shell graphics\gridGen.py”
打印(“|”+newLis[j],end=”)
索引器:列表索引超出范围

lisGen函数存在问题,它为列表生成固定数量的项(即在您的情况下为20项)。它应该是row*cols


lisGen函数存在问题,它为列表生成固定数量的项(即在您的情况下为20项)。它应该是row*cols


您的
gridGen
函数正在索引到
newLis
,但它正在测试
lis
的大小,而不是
newLis
。如果您通过
gridGen
要打印的列表,而不是让它访问全局
newLis
,这将是一个更好的设计。这将使代码更易于阅读,并降低犯类似错误的几率。类似地,
lisGen
应该创建一个本地列表并返回它,而不是修改全局
newLis

您的
gridGen
比需要的复杂得多。我们可以通过创建要打印的列表的迭代器并调用该迭代器上的
next
函数来简化它。我们给出了<代码> No.<代码>一个默认的ARG:<代码> '/COD>一个包含单个空间字符的字符串,因此当列表用完的项目<代码> NeX//C>将返回空白空间。 我们不是一个接一个地打印字符串,而是在一个列表中构建每一行,然后将该行中的字符串连接成一个字符串

这是我的程序版本。我更改了名称,使其符合PEP-8样式指南

import random

random.seed(42)

def lis_gen():
    newlis = []
    length = 20
    for i in range(length):
        value = random.randint(1,9)
        newlis.append(str(value))
    return newlis

def grid_gen(lis, rows, cols):
    it = iter(lis)

    # Horizontal line
    hline = ' -' * cols
    print(hline)
    for j in range(rows):
        line = '|'.join([next(it, ' ') for i in range(cols)])
        print('|' + line + '|')
        print(hline)

ask_rows = 5
ask_cols = 7

alpha = list('abcdefghijklmnopqrstuvwxyz')
grid_gen(alpha, ask_rows, ask_cols)
print()

newlis = lis_gen()
grid_gen(newlis, ask_rows, ask_cols)
输出

 - - - - - - -
|a|b|c|d|e|f|g|
 - - - - - - -
|h|i|j|k|l|m|n|
 - - - - - - -
|o|p|q|r|s|t|u|
 - - - - - - -
|v|w|x|y|z| | |
 - - - - - - -
| | | | | | | |
 - - - - - - -

 - - - - - - -
|2|1|5|4|4|3|2|
 - - - - - - -
|9|2|7|1|1|2|4|
 - - - - - - -
|4|9|1|9|4|9| |
 - - - - - - -
| | | | | | | |
 - - - - - - -
| | | | | | | |
 - - - - - - -
请注意,我们确实不需要在这里执行
alpha=list('abcdefghijklmnopqrstuvxyz')
:如果我们传递
grid\u gen
字符串而不是列表,它将在字符串中的每个字符上迭代


grid\u gen
还有改进的余地。再多做一点工作,我们就可以使它整洁地打印出包含多个字符的字符串。第一步是扫描输入列表,找到它包含的最长字符串的长度。如果
lis
是一个字符串列表,我们可以使用

maxlen = max(map(len, lis))

您的
gridGen
函数正在索引到
newLis
,但它正在测试
lis
的大小,而不是
newLis
。如果您通过
gridGen
要打印的列表,而不是让它访问全局
newLis
,这将是一个更好的设计。这将使代码更易于阅读,并降低犯类似错误的几率。类似地,
lisGen
应该创建一个本地列表并返回它,而不是修改全局
newLis

您的
gridGen
比需要的复杂得多。我们可以通过创建要打印的列表的迭代器并调用该迭代器上的
next
函数来简化它。我们给出了<代码> No.<代码>一个默认的ARG:<代码> '/COD>一个包含单个空间字符的字符串,因此当列表用完的项目<代码> NeX//C>将返回空白空间。 我们不是一个接一个地打印字符串,而是在一个列表中构建每一行,然后将该行中的字符串连接成一个字符串

这是我的程序版本。我更改了名称,使其符合PEP-8样式指南

import random

random.seed(42)

def lis_gen():
    newlis = []
    length = 20
    for i in range(length):
        value = random.randint(1,9)
        newlis.append(str(value))
    return newlis

def grid_gen(lis, rows, cols):
    it = iter(lis)

    # Horizontal line
    hline = ' -' * cols
    print(hline)
    for j in range(rows):
        line = '|'.join([next(it, ' ') for i in range(cols)])
        print('|' + line + '|')
        print(hline)

ask_rows = 5
ask_cols = 7

alpha = list('abcdefghijklmnopqrstuvwxyz')
grid_gen(alpha, ask_rows, ask_cols)
print()

newlis = lis_gen()
grid_gen(newlis, ask_rows, ask_cols)
输出

 - - - - - - -
|a|b|c|d|e|f|g|
 - - - - - - -
|h|i|j|k|l|m|n|
 - - - - - - -
|o|p|q|r|s|t|u|
 - - - - - - -
|v|w|x|y|z| | |
 - - - - - - -
| | | | | | | |
 - - - - - - -

 - - - - - - -
|2|1|5|4|4|3|2|
 - - - - - - -
|9|2|7|1|1|2|4|
 - - - - - - -
|4|9|1|9|4|9| |
 - - - - - - -
| | | | | | | |
 - - - - - - -
| | | | | | | |
 - - - - - - -
请注意,我们确实不需要在这里执行
alpha=list('abcdefghijklmnopqrstuvxyz')
:如果我们传递
grid\u gen
字符串而不是列表,它将在字符串中的每个字符上迭代


grid\u gen
还有改进的余地。再多做一点工作,我们就可以使它整洁地打印出包含多个字符的字符串。第一步是扫描输入列表,找到它包含的最长字符串的长度。如果
lis
是一个字符串列表,我们可以使用

maxlen = max(map(len, lis))

你正在检查
如果我错过了它,谢谢你的帮助。你正在检查
如果我错过了它,谢谢你的帮助。哇,伙计,这是对我代码的伟大解释和改进。真的谢谢你:D。那条线的方法很棒,谈到网格的改进是的,我计划用你的maxlen方法来做,并尝试多数字输出。哇,伙计,这对我的代码来说是一个很好的解释和改进。真的谢谢你:D。那条线的方法很棒,谈到网格的改进是的,我计划用你的maxlen方法来做,并尝试多数字输出。