列出仅在程序运行后才知道的名称 [英] Make a list with a name that is only known after the program runs

查看:56
本文介绍了列出仅在程序运行后才知道的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列出一个列表,并命名为我在运行程序后才知道的名称:

I want to make a list and call it a name which I only know after I run the program:

例如:

#making shelfs
group_number = 1
group_name = 'group' + str(group_number)
print group_name

group_name将是:group1

group_name will be: group1

现在,我要创建一个名为group1的空列表.怎么做这样的事情?

Now I want to make an empty list called group1. How to do such a thing?

推荐答案

通常,您只需将其放入字典中即可.

Usually you just put this into a dictionary:

d = {group_name:[]}

现在,您可以通过字典访问列表.例如:

Now you have access to your list via the dictionary. e.g.:

d['group1'].append('Hello World!')


另一种方法是修改globals()函数(它是字典)的结果.这绝对是一种不好的做法,应应避免,但是我将其包含在此处,因为了解有关您使用的工具的更多信息总是很高兴的:


The alternative is to modify the result of the globals() function (which is a dictionary). This is definitely bad practice and should be avoided, but I include it here as it's always nice to know more about the tool you're working with:

globals()[group_name] = []
group1.append("Hello World!")

这篇关于列出仅在程序运行后才知道的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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