在python中,如何将整列附加到表(列表列表列表)中?

在python中,如何将整列附加到表(列表列表列表)中?,python,mysql,list,tuples,Python,Mysql,List,Tuples,给定从循环中的mysql fetchall调用生成的如下元组 tuple1 = (('doug', 6), ('fred', 9), ('garth', 3)) tuple2 = (('steve', 3), ('dan', 1)) tuple3 = (('alan', 5), ('tom', 8), ('bob', 3), ('joe', 8)) 如何将每个元组作为一整列附加到这样的表(列表列表)中 table = [['doug',6,'steve',3,'alan',5],

给定从循环中的mysql fetchall调用生成的如下元组

tuple1 = (('doug', 6), ('fred', 9), ('garth', 3))
tuple2 = (('steve', 3), ('dan', 1))
tuple3 = (('alan', 5), ('tom', 8), ('bob', 3), ('joe', 8))
如何将每个元组作为一整列附加到这样的表(列表列表)中

table = [['doug',6,'steve',3,'alan',5],
         ['fred',9,'dan',1,'tom',8],
         ['garth',3,'',,'bob',3],
         ['',,'',,'joe',8]]

由于列表的大小不同,
zip()
在这里没有用处,因此我们必须实现自己的类似于
zip
的函数,它接受不同长度的列表,用
None
填充缺少的元素:

def transpose(lists):
   if not lists: return []
   return map(lambda *row: list(row), *lists)
接下来,将所有元组粘在一个列表中:

tuple1 = (('doug', 6), ('fred', 9), ('garth', 3))
tuple2 = (('steve', 3), ('dan', 1))
tuple3 = (('alan', 5), ('tom', 8), ('bob', 3), ('joe', 8))
tuples = [tuple1, tuple2, tuple3]
答案很简单,用列表的理解来写:

table = [[y for x in t for y in x or ['']] for t in transpose(tuples)]
结果如预期:

table
=> [['doug', 6, 'steve', 3, 'alan', 5],
    ['fred', 9, 'dan', 1, 'tom', 8],
    ['garth', 3, '', 'bob', 3],
    ['', '', 'joe', 8]]
关于评论中的问题:如何向现有表添加新列?以下是方法:

def addcolumn(table, column):
    tr = transpose([table, column])
    return [(x if x else []) + (list(y) if y else []) for x, y in tr]
继续这个例子:

tuple4 = (('hewey', 1), ('dewey', 2), ('louie', 3))
addcolumn(table, tuple4)

=> [['doug', 6, 'steve', 3, 'alan', 5, 'hewey', 1],
    ['fred', 9, 'dan', 1, 'tom', 8, 'dewey', 2],
    ['garth', 3, '', 'bob', 3, 'louie', 3],
    ['', '', 'joe', 8]]

由于列表的大小不同,
zip()
在这里没有用处,因此我们必须实现自己的类似于
zip
的函数,它接受不同长度的列表,用
None
填充缺少的元素:

def transpose(lists):
   if not lists: return []
   return map(lambda *row: list(row), *lists)
接下来,将所有元组粘在一个列表中:

tuple1 = (('doug', 6), ('fred', 9), ('garth', 3))
tuple2 = (('steve', 3), ('dan', 1))
tuple3 = (('alan', 5), ('tom', 8), ('bob', 3), ('joe', 8))
tuples = [tuple1, tuple2, tuple3]
答案很简单,用列表的理解来写:

table = [[y for x in t for y in x or ['']] for t in transpose(tuples)]
结果如预期:

table
=> [['doug', 6, 'steve', 3, 'alan', 5],
    ['fred', 9, 'dan', 1, 'tom', 8],
    ['garth', 3, '', 'bob', 3],
    ['', '', 'joe', 8]]
关于评论中的问题:如何向现有表添加新列?以下是方法:

def addcolumn(table, column):
    tr = transpose([table, column])
    return [(x if x else []) + (list(y) if y else []) for x, y in tr]
继续这个例子:

tuple4 = (('hewey', 1), ('dewey', 2), ('louie', 3))
addcolumn(table, tuple4)

=> [['doug', 6, 'steve', 3, 'alan', 5, 'hewey', 1],
    ['fred', 9, 'dan', 1, 'tom', 8, 'dewey', 2],
    ['garth', 3, '', 'bob', 3, 'louie', 3],
    ['', '', 'joe', 8]]

哇,奥斯卡。。。你真是太棒了。如果我想用一个函数“addcolumn”来实现这个功能,我现在可以将tuple4添加到现有的表中,那该怎么办?@panofish当然可以,看看我更新的答案。我希望这对你有用;)奥斯卡。。。你太棒了。这正是我需要的!如果我创建一个空表,比如table=[],然后使用addcolumn添加tuple1。。。我得到一个错误“不支持的操作数类型”。addcolumn仅在添加到已包含数据的表时有效?是。事实上,如果您传递一组元组来创建表,而不是逐个递增地添加它们,那么效率会更高。。。你真是太棒了。如果我想用一个函数“addcolumn”来实现这个功能,我现在可以将tuple4添加到现有的表中,那该怎么办?@panofish当然可以,看看我更新的答案。我希望这对你有用;)奥斯卡。。。你太棒了。这正是我需要的!如果我创建一个空表,比如table=[],然后使用addcolumn添加tuple1。。。我得到一个错误“不支持的操作数类型”。addcolumn仅在添加到已包含数据的表时有效?是。事实上,如果您传递一个元组列表来创建表,而不是逐个递增地添加它们,那么效率会更高