简体   繁体   English

Python将列表插入列表中具有给定长度的列表

[英]Python inserting lists into a list with given length of the list

My problem is, that need a list with length of 6: 我的问题是,需要一个长度为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() 不要为变量使用名称list因为它与内置函数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],[],[]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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