本文介绍了我怎样才能使蟒蛇多个空数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能做出许多空数组,而无需手动键入

How can I make many empty arrays without manually typing

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

有一个for循环,这将使我'N'这样的空数组数?

Is there a for loop that will make me 'n' number of such empty arrays?

推荐答案

一个列表COM prehension是最容易在这里:

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]]

这篇关于我怎样才能使蟒蛇多个空数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 05:17
查看更多