Python 如何获取变量名的值?
Python how to get the value with variable name?
例如,我们在 LIST
.
中存储了三门课程的分数
English, Maths, Physics = 89, 92, 93
LIST = [English, Maths, Physics]
for i in range(len(LIST)):
print(LIST[i])
并且我希望打印样式类似于 English, 89
、Maths, 92
、Physics, 93
。这是定义另一个列表 LIST_name
的解决方案
English, Maths, Physics = 89, 92, 93
LIST = [English, Maths, Physics]
LIST_name = ['English', 'Maths', 'Physics']
for i in range(len(LIST)):
print(LIST_name[i], LIST[i])
我想知道是否有内置函数或其他技巧可以帮助我直接将 English
转换为 "English"
,而无需定义 LIST_name
?如果是这样,怎么办?正如 Barmar 在下面评论的那样,我正在寻找的是如何从一个值转到它来自的变量的名称?
您可以使用一种方法,将变量关键字参数作为输入并为您提供 dict
和键值对
def marks(**m):
return m
d=marks(English=90,Maths=100,Physics=90) #call method with keyword args
print(d)
输出:
{'English': 90, 'Maths': 100, 'Physics': 90}
你也可以迭代dict
for k,v in d.items():
print(k,v,sep=', ')
输出
English, 90
Maths, 100
Physics, 90
添加到@Deadpool 的答案中,您可以使用:
>>> dict(English=82,Maths=92,Physics=93)
{'English': 82, 'Maths': 92, 'Physics': 93}
>>>
更符合您的情况的是:
>>> print('\n'.join(map(lambda x: ', '.join(map(str, x)), dict(English=82,Maths=92,Physics=93).items())))
English, 82
Maths, 92
Physics, 93
>>>
定义代表您正在使用的对象的 class 并覆盖 __str__
(以及可选的 __repr__
)函数:
class Course(object):
def __init__(self, name, score):
self.name = name
self.score = score
def __str__(self):
return self.name + ', ' + str(self.score)
def __repr__(self):
return '<{0}.{1} object at {2}> {3}'.format(self.__class__.__module__,
self.__class__.__name__,
hex(id(self)), str(self))
English, Maths, Physics = list(map(lambda x:Course(*x), zip(['English','Math','Physics'],[89,92,93])))
或者,结合使用 dict
的其他建议:
English,Maths,Physics = list(map(lambda x:Course(*x), dict(English=82,Maths=92,Physics=93).items()))
然后你可以:
>>> LIST = [English, Maths, Physics]
>>> for i in LIST:
... print(i)
...
English, 89
Math, 92
Physics, 93
例如,我们在 LIST
.
English, Maths, Physics = 89, 92, 93
LIST = [English, Maths, Physics]
for i in range(len(LIST)):
print(LIST[i])
并且我希望打印样式类似于 English, 89
、Maths, 92
、Physics, 93
。这是定义另一个列表 LIST_name
English, Maths, Physics = 89, 92, 93
LIST = [English, Maths, Physics]
LIST_name = ['English', 'Maths', 'Physics']
for i in range(len(LIST)):
print(LIST_name[i], LIST[i])
我想知道是否有内置函数或其他技巧可以帮助我直接将 English
转换为 "English"
,而无需定义 LIST_name
?如果是这样,怎么办?正如 Barmar 在下面评论的那样,我正在寻找的是如何从一个值转到它来自的变量的名称?
您可以使用一种方法,将变量关键字参数作为输入并为您提供 dict
和键值对
def marks(**m):
return m
d=marks(English=90,Maths=100,Physics=90) #call method with keyword args
print(d)
输出:
{'English': 90, 'Maths': 100, 'Physics': 90}
你也可以迭代dict
for k,v in d.items():
print(k,v,sep=', ')
输出
English, 90
Maths, 100
Physics, 90
添加到@Deadpool 的答案中,您可以使用:
>>> dict(English=82,Maths=92,Physics=93)
{'English': 82, 'Maths': 92, 'Physics': 93}
>>>
更符合您的情况的是:
>>> print('\n'.join(map(lambda x: ', '.join(map(str, x)), dict(English=82,Maths=92,Physics=93).items())))
English, 82
Maths, 92
Physics, 93
>>>
定义代表您正在使用的对象的 class 并覆盖 __str__
(以及可选的 __repr__
)函数:
class Course(object):
def __init__(self, name, score):
self.name = name
self.score = score
def __str__(self):
return self.name + ', ' + str(self.score)
def __repr__(self):
return '<{0}.{1} object at {2}> {3}'.format(self.__class__.__module__,
self.__class__.__name__,
hex(id(self)), str(self))
English, Maths, Physics = list(map(lambda x:Course(*x), zip(['English','Math','Physics'],[89,92,93])))
或者,结合使用 dict
的其他建议:
English,Maths,Physics = list(map(lambda x:Course(*x), dict(English=82,Maths=92,Physics=93).items()))
然后你可以:
>>> LIST = [English, Maths, Physics]
>>> for i in LIST:
... print(i)
...
English, 89
Math, 92
Physics, 93