本文介绍如何将模板中形如“-[ ] selection one”的无序选项,自动转换为带小写字母编号的有序列表(如“a. selection one”),并适配到现有python类的`__str__`方法中,支持任意长度(注意:超出26项需扩展逻辑)。
在实际开发中,常需将原始模板中的占位式选项(如 -[ ] selection one)动态渲染为符合排版规范的有序列表。Python 提供了简洁高效的字符串与枚举处理能力,可轻松实现字母编号(a., b., c., …)。核心思路是:利用 enumerate() 获取索引,再通过 chr(ord('a') + i) 将数字映射为对应小写字母(ord('a') 为 97,故 chr(97) == 'a')。
以下是在您原有 __str__ 方法中集成该逻辑的推荐写法:
from operator import attrgetter
def __str__(self):
# 按 order 排序选项
self.options.sort(key=attrgetter('order'))
# 生成带字母编号的选项列表:a. selection one, b. selection two...
numbered_options = []
for i, option in enumerate(self.options):
letter = chr(ord('a') + i) # a → 0, b → 1, ..., z → 25
numbered_options.append(f"{letter}. {option}")
correct_selection = ", ".join([
str(selection)
for selection in self.options
if
selection.correct
])
return _TEMPLATE.safe_substitute({
'dilemma_ethos': self.text,
'options': "\n".join(numbered_options), # 替换为字母编号版本
'correct_selection': correct_selection,
})✅ 关键说明:
import string
letters = string.ascii_lowercase # ['a', 'b', ..., 'z']
# 超出时可扩展为:itertools.chain(letters, (f"{a}{b}" for a in letters for b in letters))⚠️ 注意事项:
通过上述改造,您的输出将自动从无序勾选框升级为专业、易读的字母序号列表,兼顾语义清晰性与代码健壮性。