Python 附加到具有未知维度的嵌套列表

Python 附加到具有未知维度的嵌套列表,python,list,multidimensional-array,nested,nested-lists,Python,List,Multidimensional Array,Nested,Nested Lists,我希望根据用户的需要获取嵌套列表的维度。 然后将用户给定的字符串附加到相应的元素中。 在嵌套列表中找到最大长度的字符串以正确地左对齐文本。 然后左对齐字符串,以表格形式打印字符串。 该计划应该进行这方面的工作 Enter the number of main items in the list: 3 Enter the number of sub items that each list will contain: 4 (1,1):apples (1,2):oranges (1,3):cherr

我希望根据用户的需要获取嵌套列表的维度。 然后将用户给定的字符串附加到相应的元素中。 在嵌套列表中找到最大长度的字符串以正确地左对齐文本。 然后左对齐字符串,以表格形式打印字符串。 该计划应该进行这方面的工作

Enter the number of main items in the list: 3
Enter the number of sub items that each list will contain: 4
(1,1):apples
(1,2):oranges
(1,3):cherries
(1,4):banana
(2,1):Alice
(2,2):Bob
(2,3):Carol
(2,4):David
(3,1):dogs
(3,2):cats
(3,3):moose
(3,4):goose
Following list must be created in the program
listOfList= [ ['apples', 'oranges', 'cherries', 'banana'],
              ['Alice', 'Bob', 'Carol', 'David'],
              ['dogs', 'cats', 'moose', 'goose']]
output given by a print table function should be like this:
'''
apples   Alice  dogs
oranges  Bob    cats
cherries Carol  moose
banana   David  goose
这是我的实际代码

#Organising Lists of Lists in tablular Form
x=int(input("Enter the number of main items in the list: "))
y=int(input("Enter the number of sub items that each list will contain: "))
listOfList=[[]]
for i in range(x):
    for j in range(y):
        listOfList[i][j]=input('('+str(i+1)+','+str(j+1)+'):')
def printTable(nestedList):
    maxLen=0
    #this loop finds the max Length of any string in the nestedList
    for i in range(x):
        for j in range(y):
            if len(nestedList[i][j])>maxLen:
                maxLen=len(nestedList[i][j])
    #Loop to display table
    for j in range(y):
        for i in range(x):
            print(nestedList[i][j].ljust(maxLen),sep=' ', end='')
        print()
printTable(listOfList)
发生错误:

Enter the number of main items in the list: 3
Enter the number of sub items that each list will contain: 4
(1,1):apples
Traceback (most recent call last):
  File "C:\pyscripts\printTable.py", line 7, in <module>
    listOfList[i][j]=input('('+str(i+1)+','+str(j+1)+'):')
IndexError: list assignment index out of range
输入列表中主要项目的数量:3
输入每个列表将包含的子项数:4
(1,1):苹果
回溯(最近一次呼叫最后一次):
文件“C:\pyscripts\printTable.py”,第7行,在
listOfList[i][j]=输入('('+str(i+1)+','+str(j+1)+'):')
索引器:列表分配索引超出范围

您需要先创建列表:

listOfList = [[0 for w in range(y)] for h in range(x)] 
输出:

>>> x = 3
>>> y = 4
>>> listOfList = [[0 for w in range(y)] for h in range(x)]
>>> listOfList
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

你看你的列表有问题

>>>len(listOfList)
1
但是

这意味着您的内部列表为空,因此无法访问listOfList[0][0]

 >>>listOfList[0][0]
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 IndexError: list index out of range
列表[0][0] 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 索引器:列表索引超出范围
其他答案指出了一个关键问题,即您没有在列表中预先分配空间,尝试分配给不存在的索引将导致出现错误,但是:

您可以通过在输入的列表comp中构建列表来避免预先初始化列表,例如:

x = int(input('X: '))
y = int(input('Y: '))

lol = [
    [input('({},{}): '.format(b, a)) for a in range(1,  y + 1)]
    for b in range(1, x + 1)
]
然后,您可以使用以下方法计算最大字长,而无需显式循环并跟踪最大值:

max_length = max(len(word) for lst in lol for word in lst)
然后,您可以使用
zip
转换行/列,并通过将单词padding连接到最大长度来打印每行,而不是在反向索引上循环,例如:

for line in zip(*lol):
    print(' '.join(word.ljust(max_length) for word in line))
这将为您提供:

apples   Alice    dogs    
oranges  Bob      cats    
cherries Carol    moose   
banana   David    goose

初始化列表并使用它。您正试图在空列表中添加索引。我们不能在空列表中分配索引。尝试初始化你会得到答案。
apples   Alice    dogs    
oranges  Bob      cats    
cherries Carol    moose   
banana   David    goose