如何填写python中的列表列表?
说我在循环中并且有一个返回的函数:
returned_list = [ 2, 7, 4]
我想创建一个新的列表列表,如下所示:
listoflist = [[2, 0, 0],
[7, 0, 0],
[4, 0, 0]]
在循环的下一次迭代中,假设returned_list为:
returned_list = [12, 44, 9]
然后listoflist将更新为:
listoflist = [[2, 12, 0],
[7, 44, 0],
[4, 9, 0]]
如何定义listoflist以及如何从returned_list获取值并在Python 2中按顺序放置?
答案 0 :(得分:1)
您需要使用list.append()函数将列表附加到 listoflist 。
listoflist = []
while(...) {
returned_list = function() #This function returns the list
listoflist.append(returned_list)
}