如何创建一个空列表的列表 [英] How to create a list of empty lists

查看:307
本文介绍了如何创建一个空列表的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果以前已经回答过这个问题,但是我在这里找不到类似的问题.

Apologies if this has been answered before, but I couldn't find a similar question on here.

我对Python还是很陌生,我想创建的内容如下:

I am pretty new to Python and what I am trying to create is as follows:

list1 = []
list2 = []
results = [list1, list2]

这段代码绝对可以正常工作,但是我想知道是否有一种更快的方法可以在一行中做到这一点.

This code works absolutely fine, but I was wondering if there was a quicker way to do this in one line.

我尝试了以下方法,但没有用,但我希望它能说明我所追求的东西:

I tried the following, which didn't work, but I hope it demonstrates the sort of thing that I'm after:

result = [list1[], list2[]]

就复杂性而言,将其放在一行上真的会有所不同吗?还是在两种情况下都是三个作业?

Also, in terms of complexity, would having it on one line really make any difference? Or would it be three assignments in either case?

推荐答案

对于手动创建指定数量的列表,这样做会很好:

For manually creating a specified number of lists, this would be good:

empty_list = [ [], [], ..... ]

如果您想生成更多列表,那么将其放入for循环中会很好:

In case, you want to generate a bigger number of lists, then putting it inside a for loop would be good:

empty_lists = [ [] for i in range(n) ]

这篇关于如何创建一个空列表的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆