Python - 如何在末尾生成带有新数字的变量列表 [英] Python - how to generate list of variables with new number at end

查看:26
本文介绍了Python - 如何在末尾生成带有新数字的变量列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是找到一种更简洁的方法来创建彼此相似的空列表变量,但末尾的数字不同.

What I would like to do is find a more concise way of creating variables of empty lists that will be similar to each other, with the exception of different numbers at the end.

#For example:
var1 = []
var2 = []
var3 = []
var4 = []
#...
varN = []

#with the end goal of:
var_list = [var1,var2,var3,var4, ... varN]

推荐答案

使用 列表理解:

[[] for n in range(N)]

或者一个简单的 for 循环

or a simple for loop

empt_lists = []
for n in range(N):
    empt_lists.append([])

请注意,[[]]*N 不起作用,它将对所有项目使用相同的指针!!!

Note that [[]]*N does NOT work, it will use the same pointer for all the items!!!

这篇关于Python - 如何在末尾生成带有新数字的变量列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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