在Python中,如何在循环中创建大量变量? [英] In Python, how to create a large number of variables in a loop?

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

问题描述

我正在python 3.2中寻找一种创建大量变量并为其分配值的方法.像

I'm looking for a way, in python 3.2, to create a large number of variables and assign them values. Something like

X = 10
Y = 10
A = 0
B = 0
while X >= 0:
    while Y >= 0:
        cell[C]X[A] = A
        cell[C]Y[B] = B
        B = B + 1
        Y = Y - 1
        C = C + 1
    A = A + 1
    X = X - 1

最佳将创建200个变量,分别为cell1X1,cell1Y1,cell2X1,cell2Y2等,等等.

Which would optimally create 200 variables of cell1X1, cell1Y1, cell2X1, cell2Y2, etc etc etc.

这可能吗?怎么办?

请记住,我对python还是很陌生,所以请使事情尽可能简单.

Please keep in mind that I'm still very new to python, so please make things as simple as possible.

此外,尽管我了解还有其他方法可以做到这一点,并且它们可能更好,但我仍然想知道如何以这种方式做事.

Also, while I understand that there are other ways to do this, and they they are probably better, I still want to know how to do things this way.

我知道词典可能在所有可能的方面都更好,但这不是我要的.

I understand dictionaries might be better in every possible way, but that's not what I'm asking for.

感谢您的帮助.

当我对python说新手时,我的意思是一般地说对编程来说是新手.好像很新.就像最近刚学会如何编写函数一样.

When I say new to python, I meant to say new to programming in general. Like very new. Like, just recently learned how to write functions.

推荐答案

天黑了,但是您可以尝试:

It's dark and hacky, but you could try:

 for i in range(100):
     locals()['A%i'%i] = i

但是,请不要执行此操作.这通常是可怕的做法.

But please don't do this. It's usually terrible practice.

正如@delnan指出的那样,它可能不适用于某些python版本和实现. locals()是本地名称空间,它是Python实现的一部分.您真的不想玩它.

As @delnan points out, it probably won't work on some python versions and implementations. locals() is the local namespace, which is part of Python's implementation. You don't really want to play with it.

您不想这样做的另一个更重要的原因-这是安全性和可靠性的噩梦.您看不到所有变量名都是什么,因此很可能会意外覆盖其他变量之一.您将从同一位置获得所有这些变量",因此将它们全部放到字典中是合乎逻辑的.

Another more important reason why you don't want to do this - it's a security and reliability nightmare. You can't see what all the variable names will be, so it's possible you will accidentally overwrite one of your other variables. You are getting all these "variables" from the same place, so it's logical to put them all together in a dictionary.

至少在某些情况下,我将解释它为何起作用:本地名称空间通常被实现为字典,而locals()有时会返回本地名称空间字典,而不是副本.

I'll explain why it works, at least in some cases: the local namespace is often implemented as a dictionary, and locals() will sometimes return the local namespace dictionary, not a copy.

那根本没有道理吗?

Python变量就像字典中的项目.模块有一个命名空间,函数有一个命名空间,一个全局"命名空间,依此类推. locals()为您提供最本地的名称空间,而globals()为您提供全局的名称空间.

Python variables are like items in a dictionary. There's one namespace for the module, one for the function, one "global" namespace, and so on. locals() gets you the most local namespace, and globals() gets you the global namespace.

如果本地名称空间实际上是作为字典实现的,则可以通过编辑locals()(进入python解释器的内胆)对其进行编辑.但是,有时本地名称空间未实现为字典(出于性能原因)-这种情况发生在函数内部.在这种情况下,locals()将为您提供名称空间的副本,并且您将无法对其进行编辑(或者至少不会更改本地名称空间).

If the local namespace is actually implemented as a dictionary, you may be able edit it by editing locals() (reaching into the guts of the python interpreter). HOWEVER, sometimes the local namespace is not implemented as a dictionary (for performance reasons) - this happens inside functions. In that case, locals() will give you a copy of the namespace, and you will not be able to edit it (or at least, it won't change the local namespace).

仍然感到困惑吗?不要这样做.

Still confused? Don't do it.

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

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