简体   繁体   English

如何在 Python 中创建多个空列表?

[英]How can I make multiple empty lists in Python?

How can I make many empty lists without manually typing the following?如何在不手动输入以下内容的情况下制作许多空列表?

list1=[], list2=[], list3=[]

Is there a for loop that will make me 'n' number of such empty lists?是否有一个for循环可以让我有“n”个这样的空列表?

A list comprehension is easiest here:列表理解在这里最简单:

>>> n = 5
>>> lists = [[] for _ in range(n)]
>>> lists
[[], [], [], [], []]

Be wary not to fall into the trap that is:小心不要落入以下陷阱:

>>> lists = [[]] * 5
>>> lists
[[], [], [], [], []]
>>> lists[0].append(1)
>>> lists
[[1], [1], [1], [1], [1]]

如果您想创建没有“列表列表”的不同列表,请尝试以下操作:

list1, list2, list3, list4 = ([] for i in range(4))

Look up list comprehensions :查找列表推导

listOfLists = [[] for i in range(N)]

Now, listOfLists has N empty lists in it.现在, listOfLists中有 N 个空列表。

More links on list comprehensions:有关列表推导的更多链接:

1 2 3 1 2 3

I see that many answers here get you many lists with no names assigned (as elements of a larger list).我看到这里的许多答案会为您提供许多未指定名称的列表(作为较大列表的元素)。 This may be not always what's needed.这可能并不总是需要的。

It is possible to create multiple empty lists, each with a different name/value, using a tuple:可以使用元组创建多个空列表,每个列表具有不同的名称/值:

a,b,c = ([],[],[])

Changing this structure should get you what you want.改变这个结构应该得到你想要的。

Use this:用这个:

def mklist(n):
    for _ in range(n):
        yield []

Usage:用法:

list(mklist(10))
[[], [], [], [], [], [], [], [], [], []]

a, b, c = mklist(3) # a=[]; b=[]; c=[]

The code below will dynamically 'create' itself.下面的代码将动态地“创建”自身。 For every iteration, the following command will issued:对于每次迭代,将发出以下命令:

list number = []列表编号= []

Where number is the value of i in the loop.其中number是循环中i的值。

x = 3 # Amount of lists you want to create
for i in range(1, x+1):
    command = "" # This line is here to clear out the previous command
    command = "list" + str(i) + " = []"
    exec(command)

The result of this particular piece of code is three variables: list1, list2 and list3 being created and each assigned an empty list.这段特定代码的结果是创建了三个变量:list1、list2 和 list3,每个变量都分配了一个空列表。

You can generalize this to make practically anything you want.您可以将其概括为几乎任何您想要的东西。 Do note that you cannot put this into a function as far as I know, because of how variables and global variables work.请注意,据我所知,您不能将其放入函数中,因为变量和全局变量是如何工作的。

name = "my_var" # This has to be a string, variables like my_var1, my_var2 will be created.
value = "[]" # This also has to be a string, even when you want to assign integers! When you want to assign a string "3", you'd do this: value = "'3'"
amount = 5 # This must be an integer. This many variables will be created (my_var1, my_var2 ... my_var5).

for i in range(1, amount+1):
    command_variable = ""
    command_variable = name + str(i) + " = " + value
    exec(command_variable)

You can also add a list of list of list of list... like this too.您还可以添加列表列表列表列表......也像这样。 It won't return any error.它不会返回任何错误。 But I haven't got any idea what it's used for.但我不知道它是做什么用的。

n = 5 
>>> lists = [[[]]] for _ in range(n)]
>>> lists
   [[[[]]], [[[]]], [[[]]], [[[]]], [[[]]]]

or as simple as或者简单到

 added, modified, removed, inactive = ([],) * 4

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

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