2017-04-26 42 views
1

我编写了这段代码,以网格形式显示列表的内容。 它适用于字母表列表。 但是,当我尝试使用随机生成的列表运行它时,它会给列表索引超出范围错误。我在处理我的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','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) 

预期的/正确的输出,使用字母表列表(LIS):

Enter number of rows :7 
Enter number of columns :7 
- - - - - - - 
|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):

Enter number of rows :7 
Enter number of columns :7 
- - - - - - - 
|9|2|1|4|7|5|4| 
- - - - - - - 
|9|7|7|3|2|1|3| 
- - - - - - - 
|7|5|4|1|2|3Traceback (most recent call last): 
    File "D:\01-Mywares\python\2d shell graphics\gridGen.py", line 56, in <module> 
    gridGen(askRow,askColumns) 
    File "D:\01-Mywares\python\2d shell graphics\gridGen.py", line 40, in gridGen 
    print('|'+newLis[j],end='') 
IndexError: list index out of range 
+0

你所检查的'如果j

+0

我错过了,谢谢你的帮助。 – USBEN

回答

0

gridGen功能进行索引newLis但它测试的lis代替newLis大小。如果您通过gridGen您想要打印的列表,而不是让其访问全球newLis,那将是一个更好的设计。这将使代码更易于阅读,并减少这种错误的可能性。同样,lisGen应该创建一个本地列表并返回它,而不是改变全局newLis

你的gridGen比它需要更复杂。我们可以通过创建要打印的列表的迭代器并在该迭代器上调用next函数来简化它。我们给next默认参数' ' - 一个包含单个空格字符的字符串,所以当列表中的项目用完时next将返回空格。

我们不是逐个打印字符串,而是在列表理解中构建每行,然后将行中的字符串连接到单个字符串中。

这是我的程序版本。我更改了名称,使其符合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('abcdefghijklmnopqrstuvwxyz')这里:如果我们通过grid_gen一个字符串,而不是一个列表它会遍历字符串中的字符各的。


grid_gen还有改进的空间。再多做一点工作,我们可以整齐地打印出包含多个字符的字符串。第一步是扫描输入列表以查找其包含的最长字符串的长度。如果lis是一个字符串列表,我们可以做到这一点与

maxlen = max(map(len, lis)) 
+0

哇,这是对我的代码很好的解释和改进。真的非常感谢你:D。该行方法很好,并谈论grid_len改进是的,我打算用你的maxlen方法来做到这一点,并尝试多位输出。 – USBEN

0

lisGen函数的问题,它是一般的用于列表的固定数量的项目(即20例)。它应该是行*列。

This is the link for updated code

+0

好的,解决这个问题:D,谢谢你。 – USBEN