且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Python中创建一个空列表或空列表元组?

更新时间:2022-12-08 23:21:51

result = [list(someListOfElements) for _ in xrange(x)]

这将创建x个不同的列表,每个列表都有一个someListOfElements列表的副本(该列表中的每个项目均作为参考,但其中的列表是副本).

This will make x distinct lists, each with a copy of someListOfElements list (each item in that list is by reference, but the list its in is a copy).

如果更有意义,请考虑使用copy.deepcopy(someListOfElements)

If it makes more sense, consider using copy.deepcopy(someListOfElements)

生成器和列表理解以及事物被认为是 pythonic .

Generators and list comprehensions and things are considered quite pythonic.