使用字符串的枚举迭代列表

huangapple go评论63阅读模式
英文:

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 = &#39;colorred&#39;
       COLORBLUE = &#39;colorblue&#39;

    itemList = [&lt;myenum.COLORRED,&#39;colorred&#39;&gt;]

I am trying to get value from itemList &#39;coloured&#39; but `itemList.value` gives a type check error in Python with the error message &quot;str has no attribute value&quot;. 





  

list(map(str,myenum)) gives key COLORRED but I need the value &#39;coloured&#39;. 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: &#39;myEnum&#39;
    
    myEnum:
      type: String
      enum:
        - &#39;state1&#39;
        - &#39;state2&#39;

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 = &quot;color red&quot;
    BLUE = &quot;color blue&quot;

lst = [Color.RED, Color.BLUE]
expected = [&#39;color red&#39;, &#39;color blue&#39;]

# 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 = &#39;red&#39;
GREEN = &#39;green&#39;
BLUE = &#39;blue&#39;
print(Color.RED) #=&gt; 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 = &#39;colorred&#39;
COLORBLUE = &#39;colorblue&#39;
mylist = [x.value for x in myenum]
# where mylist will be [&#39;colorred&#39;, &#39;colorblue&#39;]

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.

huangapple
  • 本文由 发表于 2023年6月15日 12:50:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定