我想在Python中动态创建变量。有人有什么创造性的方法吗?
当前回答
vars()['meta_anio_2012'] = 'translate'
其他回答
在全局和/或本地名称空间中填充内容并不是一个好主意。使用字典太像其他语言了……D ['constant-key'] = value看起来很尴尬。Python是面向对象的。用一位大师的话来说:“命名空间是一个非常棒的想法——让我们做更多这样的事情吧!”""
是这样的:
>>> class Record(object):
... pass
...
>>> r = Record()
>>> r.foo = 'oof'
>>> setattr(r, 'bar', 'rab')
>>> r.foo
'oof'
>>> r.bar
'rab'
>>> names = 'id description price'.split()
>>> values = [666, 'duct tape', 3.45]
>>> s = Record()
>>> for name, value in zip(names, values):
... setattr(s, name, value)
...
>>> s.__dict__ # If you are suffering from dict withdrawal symptoms
{'price': 3.45, 'id': 666, 'description': 'duct tape'}
>>>
Globals()返回模块变量的字典。你可以通过在字典上创建一个键来创建一个新变量:
# By default, a module has some hidden variables defined
print({k: v for k, v in globals().items() if not k.startswith("__")})
for i in range(1, 11):
globals()[f"my_variable_{i}"] = i
print()
print(my_variable_1)
print(my_variable_2)
# and so on
print()
print({k: v for k, v in globals().items() if not k.startswith("__")})
结果:
{}
1
2
{'i': 10, 'my_variable_1': 1, 'my_variable_2': 2, 'my_variable_3': 3, 'my_variable_4': 4, 'my_variable_5': 5, 'my_variable_6': 6, 'my_variable_7': 7, 'my_variable_8': 8, 'my_variable_9': 9, 'my_variable_10': 10}
除非非常需要创建一堆变量名,否则我会使用字典,在字典中您可以动态地创建键名并将值关联到每个键名。
a = {}
k = 0
while k < 10:
# dynamically create key
key = ...
# calculate value
value = ...
a[key] = value
k += 1
collections模块中还有一些有趣的数据结构可能适用。
关键字参数允许将变量从一个函数传递到另一个函数。通过这种方式,您可以使用字典的键作为变量名(可以在while循环中填充)。当它被调用时,字典名只需要在前面加上**。
# create a dictionary
>>> kwargs = {}
# add a key of name and assign it a value, later we'll use this key as a variable
>>> kwargs['name'] = 'python'
# an example function to use the variable
>>> def print_name(name):
... print name
# call the example function
>>> print_name(**kwargs)
python
没有**,kwargs只是一个字典:
>>> print_name(kwargs)
{'name': 'python'}
注意:这应该被认为是一个讨论,而不是一个实际的答案。
一个近似的方法是在你想要创建变量的模块中操作__main__。例如,有一个b.py:
#!/usr/bin/env python
# coding: utf-8
def set_vars():
import __main__
print '__main__', __main__
__main__.B = 1
try:
print B
except NameError as e:
print e
set_vars()
print 'B: %s' % B
运行它会输出
$ python b.py
name 'B' is not defined
__main__ <module '__main__' from 'b.py'>
B: 1
但是这种方法只适用于单个模块脚本,因为__main__ it导入将始终表示python正在执行的入口脚本的模块,这意味着如果B .py被其他代码涉及,B变量将在入口脚本的范围内创建,而不是在B .py本身中。假设有一个脚本a.py:
#!/usr/bin/env python
# coding: utf-8
try:
import b
except NameError as e:
print e
print 'in a.py: B', B
运行它会输出
$ python a.py
name 'B' is not defined
__main__ <module '__main__' from 'a.py'>
name 'B' is not defined
in a.py: B 1
注意,__main__被更改为'a.py'。
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if
- 如何在Python中获得所有直接子目录