且构网

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

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

更新时间:2022-12-08 23:07: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.