英文:
iterating list with enum of string
问题
以下是翻译好的部分:
我有一个
myenum(str, Enum):
COLORRED = 'colorred'
COLORBLUE = 'colorblue'
itemList = [<myenum.COLORRED,'colorred'>]
我试图从itemList中获取'coloured'的值,但`itemList.value`在Python中会引发类型检查错误,错误消息为"str has no attribute value"。
list(map(str, myenum))会返回键COLORRED,但我需要值'coloured'。我尝试了https://stackoverflow.com/questions/58608361/string-based-enum-in-python,但都对我不起作用。
我的枚举是由openapi自动生成的。以下是它的yaml。
我收到myElements数组,并需要myEnum中的一个值。
myElements:
type: array
items:
allOf:
- $ref: 'myEnum'
myEnum:
type: String
enum:
- 'state1'
- 'state2'
注意:问题出在Python的类型检查上,而不是在迭代值上。我需要在不使用`.values`或`.name`的情况下获取枚举值。
<details>
<summary>英文:</summary>
I have a
myenum(str, Enum):
COLORRED = 'colorred'
COLORBLUE = 'colorblue'
itemList = [<myenum.COLORRED,'colorred'>]
I am trying to get value from itemList 'coloured' but `itemList.value` gives a type check error in Python with the error message "str has no attribute value".
list(map(str,myenum)) gives key COLORRED but I need the value 'coloured'. I tried https://stackoverflow.com/questions/58608361/string-based-enum-in-python but none of them are working for me.
My enum is auto generated by openapi. below is yaml for it.
I receive array of myElements and need one value in myEnum.
myElements:
type: array
items:
allOf:
- $ref: 'myEnum'
myEnum:
type: String
enum:
- 'state1'
- 'state2'
Note: Issue is with typecheck in python not iterating the values. I need enum values without using .values or .name
</details>
# 答案1
**得分**: 3
看起来你试图使用列表的`.value`属性,而不是它的元素。
```python
from enum import Enum
class Color(Enum):
RED = "color red"
BLUE = "color blue"
lst = [Color.RED, Color.BLUE]
expected = ['color red', 'color blue']
# 这等同于 `list(map(lambda v: v.value, lst))`
values = [item.value for item in lst]
assert values == expected
英文:
Looks like you're trying to use the list's .value
property, not its elements.
from enum import Enum
class Color(Enum):
RED = "color red"
BLUE = "color blue"
lst = [Color.RED, Color.BLUE]
expected = ['color red', 'color blue']
# This is equivalent to `list(map(lambda v: v.value, lst))`
values = [item.value for item in lst]
assert values == expected
答案2
得分: 0
如果您正在使用Python 3.11,您可以使用StrEnum,这样您可以在不调用.value
的情况下获取值。
class Color(StrEnum):
RED = 'red'
GREEN = 'green'
BLUE = 'blue'
print(Color.RED) # => red
还有IntEnum。
英文:
Your point is not exactly clear to me, but...
If you are using Python 3.11 you can use StrEnum, so you can get the value without calling .value
.
class Color(StrEnum):
RED = 'red'
GREEN = 'green'
BLUE = 'blue'
print(Color.RED) #=> red
There is also IntEnum.
答案3
得分: 0
I believe what you are after is the values of the enum in a list. If so, this may be something you can work with.
from enum import Enum
class myenum(str, Enum):
COLORRED = 'colorred'
COLORBLUE = 'colorblue'
mylist = [x.value for x in myenum]
# where mylist will be ['colorred', 'colorblue']
However, if you explain how you generate your list and how you would like to use the data (perhaps with a simple example), that might help the people to understand your objectives better.
英文:
I believe what you are after is the values of the enum in a list. If so, this may be something you can work with.
from enum import Enum
class myenum(str, Enum):
COLORRED = 'colorred'
COLORBLUE = 'colorblue'
mylist = [x.value for x in myenum]
# where mylist will be ['colorred', 'colorblue']
However, if you explain how you generate your list and how you would like to use the data (perhaps with a simple example), that might help the people to understand your objectives better.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论