我希望能够将变量的名称作为字符串获取,但我不知道Python是否具有那么多的内省功能.就像是:
>>> print(my_var.__name__)
'my_var'
Run Code Online (Sandbox Code Playgroud)
我想这样做因为我有一堆vars我想变成一本字典,如:
bar = True
foo = False
>>> my_dict = dict(bar=bar, foo=foo)
>>> print my_dict
{'foo': False, 'bar': True}
Run Code Online (Sandbox Code Playgroud)
但我想要比这更自动的东西.
Python中有locals()
和vars()
,所以我想有一种方法.
这个主题讨论如何在Python中将函数的名称作为字符串获取:如何在Python中将 函数名称作为字符串获取?
如何为变量做同样的事情?(注意Python变量没有属性__name__
,至少在Python中retrieve_name
)
换句话说,如果我有一个变量,例如:
foo = dict()
foo['bar'] = 2
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个函数/属性,例如'foo'
:
retrieve_name(foo)
Run Code Online (Sandbox Code Playgroud)
返回字符串 __name__
既然人们在问我为什么要这样做,这里就是一个例子.我想在此列表中在Pandas中创建一个DataFrame,其中列名 由实际字典的名称给出:
# List of dictionaries for my DataFrame
list_of_dicts = [n_jobs, users, queues, priorities]
Run Code Online (Sandbox Code Playgroud) 假设我有一个名为choice
它的变量等于2.我如何访问变量的名称?相当于的东西
In [53]: namestr(choice)
Out[53]: 'choice'
Run Code Online (Sandbox Code Playgroud)
用于制作字典.有一个很好的方法来做到这一点,我只是错过了它.
编辑:
因此,这样做的原因.我正在运行一些数据分析的东西,我在程序中调用了多个参数,我想在运行时调整或不调整.我读了我在上一次运行中使用的参数,从.config文件格式化为
filename
no_sig_resonance.dat
mass_peak
700
choice
1,2,3
Run Code Online (Sandbox Code Playgroud)
当提示输入值时,将显示先前使用的值,空字符串输入将使用先前使用的值.
我的问题是因为在写字典时已经扫描了这些值.如果需要一个参数,我运行get_param
访问文件并找到参数.
我想我会通过阅读来避免这个问题.config
提交一次并从中生成字典.我原本避免了这个......我不再记得的原因.更新我的代码的完美情况!
如何获取DataFrame的名称并将其作为字符串打印?
例:
boston
(分配给csv文件的var名称)
boston = read_csv('boston.csv')
print ('The winner is team A based on the %s table.) % boston
Run Code Online (Sandbox Code Playgroud) 在调试时,我们经常会看到如下的print语句:
print x # easy to type, but no context
print 'x=',x # more context, harder to type
12
x= 12
Run Code Online (Sandbox Code Playgroud)
如何编写一个函数来获取变量或变量的名称并打印其名称和值?我只对调试输出感兴趣,这不会被合并到生产代码中.
debugPrint(x) # or
debugPrint('x')
x=12
Run Code Online (Sandbox Code Playgroud) 有没有办法在运行时知道变量的名称(来自代码)?或编译时遗忘的var名称(字节代码与否)?
例如
>>> vari = 15 >>> print vari.~~name~~() 'vari'
注意:我说的是普通的数据类型变量(int,str,list ...)
我有类似的问题,但我不知道这个术语是什么
cat=5
dog=3
fish=7
animals=[cat,dog,fish]
for animal in animals:
print animal_name+str(animal) #by animal name, i mean the variable thats being used
Run Code Online (Sandbox Code Playgroud)
它会打印出来,
cat5
dog3
fish7
Run Code Online (Sandbox Code Playgroud)
所以我想知道是否有一个实际的方法或函数可以用来检索正在使用的变量并将其转换为字符串.希望这可以在不为每只动物实际创建字符串的情况下完成.
编辑:
没有字典有没有办法做到这一点?
我想知道如何找到字典元素的变量名称:
例如:
>>>dict1={}
>>>dict2={}
>>>dict1['0001']='0002'
>>>dict2['nth_dict_item']=dict1
>>>print dict2
{'nth_dict_item': {'0001': '0002'}}
>>>print dict2['nth_dict_item']
{'001': '002'}
Run Code Online (Sandbox Code Playgroud)
我怎样才能告诉我dict2 ['nth_dict_item']是或正在引用"dict1"?我想要它引用的数据结构的名称而不是数据本身.
如果我将id(dict1)的输出与id(dict2 ['nth_dict_item'])进行比较,我发现它们是相同的.
但是,如何将该ID转换为变量名?是否有更直接/更清晰的方法来获取我想知道的信息?
我敢肯定我只是忽略了一个能让我的生活轻松的功能,但我对Python很陌生:)
任何帮助表示赞赏,谢谢!
更新:这就是为什么我希望这个工作:
我正在尝试制作一个使用类似数据库的字典的应用程序.我希望这个伪代码的功能起作用:
dict_1={}
dict_2={}
dict_3={}
dict_1["FooBar1.avi"]=[movie_duration,movie_type,comments]
dict_2["FooBar2.avi"]=[movie_duration,movie_type,comments]
dict_3["FooBar3.avi"]=[movie_duration,movie_type,comments]
dict_database[SomeUniqueIdentifier1]=dict_1
dict_database[SomeUniqueIdentifier2]=dict_2
dict_database[SomeUniqueIdentifier3]=dict_3
Run Code Online (Sandbox Code Playgroud)
SomeUniqueIdentifier#将是一个唯一值,我将其用作数据库键/ unqiueID来查找条目.
我希望能够通过以下方式更新FooBar1.avi的"评论"字段:
WhichDict= dict_database[SomeUniqueIdentifier1]
WhichDict[WhichDict.keys()[0]][2]='newcomment'
Run Code Online (Sandbox Code Playgroud)
而不是必须做:
dict_database['SomeUniqueIdentifier1'][dict_database['SomeUniqueIdentifier1'].keys()[0]][2]='newcomment'
Run Code Online (Sandbox Code Playgroud)
感谢大家.我现在明白我误解了很多基础知识(全脑屁).将返回并修复设计.谢谢大家!