简体   繁体   English

创建空列表的列表-Python

[英]creating list of empty lists - Python

test = [[None for e in range(1)] for e in range(len(foobar)/2)]
for delete in range(0,len(test)):
        del test[delete][0]

This is not the most pythonic way to create an empty list 这不是创建empty list的最pythonic方法

would you suggest something else? 你还能提出其他建议吗?

I know that this question explains it. 我知道这个问题可以解释。 but this is not a duplicate of the previous one as you can see. 但这不是您看到的上一个的重复。

Does this do what you want? 这是您想要的吗?

test = [[] for e in range(len(foobar)/2)]

It has the same output as your code 它的输出与您的代码相同

test = [[None for e in range(1)] for e in range(len(foobar)/2)]

can be simplified to: 可以简化为:

test = [[] for i in xrange(len(foobar)/2)]

Also don't forget to use xrange instead of range , as it returns a generator and not a complete list 同样不要忘记使用xrange而不是range ,因为它会返回生成器而不是完整列表

An empty list: 空列表:

test = []

Unless we're missing something ;-) 除非我们缺少什么;-)

Or you want an list of empty lists of a given length? 还是想要一个给定长度的空列表?

n = 100
test = [ [] for i in range(0,n) ]
test = map(lambda x: [], foobar[:len(foobar)/2])

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

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