我是Python的新手,我很好奇,如果给出:
ENROLLMENT_STATUS = (
(1, 'Enrolled'),
(2, 'Promoted'),
(3, 'Dropped'),
(4, 'Failed'),
)
我可以将整数1..4转换为str对应,而不使用如下的循环:
for i in Enrollment.ENROLLMENT_STATUS:
if i[0] == self.enrollment_status:
status = i[1]
break
感谢大家的帮助。
此致 乍得
答案 0 :(得分:3)
答案 1 :(得分:3)
你可以将它们转换成这样的字典
try:
{k: v for k, v in ENROLLMENT_STATUS}[self.enrollment_status]
except ValueError:
raise # or whatever
或者像这样:
try:
dict(ENROLLMENT_STATUS)[self.enrollment_status]
except ValueError:
raise # or whatever