gpt-4-0314· openai
Suzhen/CodeChat
python的枚举在不使用私有属性的前提下有办法将name和value存到一个字典中吗
- User
python的枚举在不使用私有属性的前提下有办法将name和value存到一个字典中吗
- Assistant
当然可以。可以这样做:
from enum import Enum class Color(Enum): RED = 1 GREEN = 2 BLUE = 3 color_dict = {color.name: color.value for color in Color} print(color_dict)输出:
{'RED': 1, 'GREEN': 2, 'BLUE': 3}这将把枚举中的名称和值存储在一个字典中,无需访问私有属性。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.