简体   繁体   中英

How do I fill up a list of lists in python?

How do I fill up a list of lists in python?

Say I'm in a loop and have a function which returns:

   returned_list = [ 2, 7, 4]

I want to create a new list of lists which looks like:

listoflist = [[2, 0, 0],
              [7, 0, 0],
              [4, 0, 0]]

In the next iteration of the loop, say the returned_list is:

returned_list = [12, 44, 9]

Then the listoflist would be updated as:

listoflist = [[2, 12, 0],
              [7, 44, 0],
              [4, 9, 0]]

How do I define listoflist and how do I take values from returned_list and put them in that order in Python 2?

You need to use the list.append() function to append the list to your listoflist .

    listoflist = []

    while(...) {
       returned_list = function()  #This function returns the list
       listoflist.append(returned_list)    
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM