Python:使用普通字符串以外的其他东西创建变量? [英] Python: Create a variable using something other than a plain string?

查看:39
本文介绍了Python:使用普通字符串以外的其他东西创建变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在另一个问题中,我尝试使用 enumerate 和 for 元素来创建一个使用元素作为名称的 Pandas 数据框.事实证明,这个问题更普遍.无论如何要使用普通字符串以外的其他东西来设置变量值?在 Bash 中,这是通过 Read 命令完成的,该命令将先前的输出带入一个子 shell 并将其分配给一个变量名(我将在下面发布我的问题).

In a different question I tried to use an enumerate and for element to create a pandas dataframe using the element as the name. It turns out the problem is more general. Is there anyway to set a variable value using something other than a plain string? In Bash this is done with the Read command, which takes the previous output into a subshell and assigns it to a variable name (I'll post my question on that below).

有什么办法可以在 Python 中做到这一点?即,一些简单的东西,如:

Any way to do this in Python? i.e., something simple like:

list1[0] = pd.dataframe(data),其中 list1[0] 是一个字符串.或者类似地使用 dict 键,其中值是一个数据帧:

list1[0] = pd.dataframe(data), where list1[0] is a string. Or similarly using a dict key where the value is a dataframe:

for i in dict1: i = dict1[key]for i in dict1: function(i) = dict1[key]?

后者不能使用 str() 或任何函数工作,因为错误抱怨SyntaxError:无法分配给函数调用",但也许有类似的东西?

The latter doesn't work using str() or any function because the error complains "SyntaxError: can't assign to function call", but maybe something similar?

Python:我如何使用将元素枚举为字符串?

https://unix.stackexchange.com/questions/338000/bash-assign-output-of-pipe-to-a-variable

https://unix.stackexchange.com/questions/574920/bash-how-to-get-stdout-to-console-and-also-pipe-to-next-command

好吧,在与@juanpa.arrivillaga 进行了长时间的讨论后,他解释说字符串不是像 Bash 中的普通字符串.让我们看看我是否有这个权利......它们是存储在 dict 对象中的字符串对象.(即使整数也被存储为对象,除非使用在 Python 数组上展开的 numpy 或 pandas).在 for i in dict1: 循环中,print(i) 起作用是因为print 将在传递给它的任何对象上隐式调用 str".i = value 不是因为将变量视为您扔在对象上的名称标签",并且 i 在其 for 循环范围内被解析为 dict 对象键.我不知道为什么,但变量 'nametags' 不能是字符串对象.

Okay, well, after extended discussion with @juanpa.arrivillaga, he explained that strings are not plain strings as in Bash. Let's see if I have this right.. they are string objects stored in dict objects. (even integers are stored as objects unless using numpy or pandas which expand on Python arrays). Inside a for i in dict1: loop print(i) works because 'print will call str implicitly on any object you pass to it'. i = value does not because 'think of variables as nametags you throw on objects' and i is being resolved in its for loop scope as a dict object key. I'm not sure why, but variable 'nametags' cannot be string objects.

存在重复答案中提到的使用 globals() 类型的解决方法:

A workaround using globals() kind of mentioned in the Duplicate answers exists:

对于 dict1 中的键: globals()[key] = pd.DataFrame()

这是因为在 CPython 中,他们使用实际的 Python 对象来实现全局命名空间.可能也许使用 ctypes 来修改局部范围内的命名空间数组",因为另一种解决方法是使用 SimpleNamespace 创建一个新对象来存储变量(大概是因为它们作为对象存储,与 globals 中相同(). 总结一下,'setattr(sys.modules[_name_], var_name, var_value) 等价于 globals()[var_name] = var_value'.>

This is because in CPython, they used actual python objects to implement the global namespaces. It might be possible to 'maybe use ctypes to modify the namespace array in a local scope', as another workaround is to use SimpleNamespace to create a new object to store variables (presumably because they are stored as objects, the same as in globals(). And to wrap up, 'setattr(sys.modules[_name_], var_name, var_value) is equivalent to globals()[var_name] = var_value'.

推荐答案

我觉得这个问题可以用字典解决:

I think this issue can be solved with dictionaries:


var_names = ['your_string', 'your_second_string']

dict = {}
for i, var_name in enumerate(var_names):
    var_name = f'var_name{i}'
    dict[var_name] = pd.DateFrame([])

这篇关于Python:使用普通字符串以外的其他东西创建变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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