简体   繁体   中英

Python inserting lists into a list with given length of the list

My problem is, that need a list with length of 6:

list=[[],[],[],[],[],[]]

Ok, that's not difficult. Next I'm going to insert integers into the list:

list=[[60],[47],[0],[47],[],[]]

Here comes the real problem: How can I now extend the lists and fill them again and so on, so that it looks something like that:

list=[[60,47,13],[47,13,8],[1,3,1],[13,8,5],[],[]]

I can't find a solution, because at the beginning i do not know the length of each list, I know, they are all the same, but I'm not able to say what length exactly they will have at the end, so I'm forced to add an element to each of these lists, but for some reason i can't.

Btw: This is not a homework, it's part of a private project :)

You don't. You use normal list operations to add elements.

L[0].append(47)

Don't use the name list for your variable it conflicts with the built-in function list()

my_list = [[],[],[],[],[],[]]
my_list[0].append(60)
my_list[1].append(47)
my_list[2].append(0)
my_list[3].append(47)
print my_list # prints [[60],[47],[0],[47],[],[]]

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