如何在python中创建多个空列表? [英] How can I make multiple empty lists in python?

查看:172
本文介绍了如何在python中创建多个空列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不手动输入的情况下创建许多空列表

How can I make many empty lists without manually typing

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

是否存在for循环,将使我成为"n"个这样的空列表?

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

推荐答案

在这里最容易理解列表:

A list comprehension is easiest here:

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

当心不要落入陷阱:

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

这篇关于如何在python中创建多个空列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆