Python 如何将列表作为列插入二维列表中?

Python 如何将列表作为列插入二维列表中?,python,arrays,list,multidimensional-array,Python,Arrays,List,Multidimensional Array,给定一个列表和一个2d列表(长度可能相同,也可能不同) 我想将列表作为列附加到2d列表中,以充分管理空值 result1 = [[1,2,0, 1], [3,4,1, 2], [4,4,4, 3], [None,None,None,4]] result2 = [[1,2,0, 1], [3,4,1, 2], [4,4,4,None

给定一个列表和一个2d列表(长度可能相同,也可能不同)

我想将列表作为列附加到2d列表中,以充分管理空值

result1 = [[1,2,0,         1],
           [3,4,1,         2],
           [4,4,4,         3],
           [None,None,None,4]]

result2 = [[1,2,0,   1],
           [3,4,1,   2],
           [4,4,4,None]]
以下是我目前掌握的情况:

table = [column + [list1[0]] for column in table]
但我在使用迭代器代替
0
时遇到语法问题

我是这样想的:

table = [column + [list1[i]] for column in enumerate(table,i)]
但是我得到一个连接到tuple
TypeError
的元组。我在想,对表进行透视,然后只追加一行并向后透视可能是一个好主意,但我无法正确处理大小调整问题。

这是怎么回事

table = [column + [list1[i] if i < len(list1) else None] for i, column in enumerate(list1)]
table=[column+[list1[i]如果i
使用发电机功能和:

table = [column + [list1[i] if i < len(list1) else None] for i, column in enumerate(list1)]
from itertools import izip_longest

def add_column(lst, col):

    #create the list col, append None's if the length is less than table's length
    col = col + [None] * (len(lst)- len(col))

    for x, y in izip_longest(lst, col):
        # here the if-condition will run only when items in col are greater than 
        # the length of table list, i.e prepend None's in this case.
        if x is None:
            yield [None] *(len(lst[0])) + [y] 
        else:
            yield x + [y]            


print list(add_column(table, list1))
#[[1, 2, 0, 1], [3, 4, 1, 2], [4, 4, 4, 3], [None, None, None, 4]]
print list(add_column(table, list2))
#[[1, 2, 0, 1], [3, 4, 1, 2], [4, 4, 4, None]]