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

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

iterating list with enum of string

问题

以下是翻译好的部分:

  1. 我有一个
  2. myenum(str, Enum):
  3. COLORRED = 'colorred'
  4. COLORBLUE = 'colorblue'
  5. itemList = [<myenum.COLORRED,'colorred'>]
  6. 我试图从itemList中获取'coloured'的值`itemList.value`Python中会引发类型检查错误错误消息为"str has no attribute value"
  7. list(map(str, myenum))会返回键COLORRED但我需要值'coloured'我尝试了https://stackoverflow.com/questions/58608361/string-based-enum-in-python但都对我不起作用
  8. 我的枚举是由openapi自动生成的以下是它的yaml
  9. 我收到myElements数组并需要myEnum中的一个值
  10. myElements:
  11. type: array
  12. items:
  13. allOf:
  14. - $ref: 'myEnum'
  15. myEnum:
  16. type: String
  17. enum:
  18. - 'state1'
  19. - 'state2'
  20. 注意问题出在Python的类型检查上而不是在迭代值上我需要在不使用`.values``.name`的情况下获取枚举值
  21. <details>
  22. <summary>英文:</summary>
  23. I have a
  24. myenum(str, Enum):
  25. COLORRED = &#39;colorred&#39;
  26. COLORBLUE = &#39;colorblue&#39;
  27. itemList = [&lt;myenum.COLORRED,&#39;colorred&#39;&gt;]
  28. 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;.
  29. 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.
  30. My enum is auto generated by openapi. below is yaml for it.
  31. I receive array of myElements and need one value in myEnum.
  32. myElements:
  33. type: array
  34. items:
  35. allOf:
  36. - $ref: &#39;myEnum&#39;
  37. myEnum:
  38. type: String
  39. enum:
  40. - &#39;state1&#39;
  41. - &#39;state2&#39;
  42. Note: Issue is with typecheck in python not iterating the values. I need enum values without using .values or .name
  43. </details>
  44. # 答案1
  45. **得分**: 3
  46. 看起来你试图使用列表的`.value`属性而不是它的元素
  47. ```python
  48. from enum import Enum
  49. class Color(Enum):
  50. RED = "color red"
  51. BLUE = "color blue"
  52. lst = [Color.RED, Color.BLUE]
  53. expected = ['color red', 'color blue']
  54. # 这等同于 `list(map(lambda v: v.value, lst))`
  55. values = [item.value for item in lst]
  56. assert values == expected
英文:

Looks like you're trying to use the list's .value property, not its elements.

  1. from enum import Enum
  2. class Color(Enum):
  3. RED = &quot;color red&quot;
  4. BLUE = &quot;color blue&quot;
  5. lst = [Color.RED, Color.BLUE]
  6. expected = [&#39;color red&#39;, &#39;color blue&#39;]
  7. # This is equivalent to `list(map(lambda v: v.value, lst))`
  8. values = [item.value for item in lst]
  9. assert values == expected

答案2

得分: 0

如果您正在使用Python 3.11,您可以使用StrEnum,这样您可以在不调用.value的情况下获取值。

  1. class Color(StrEnum):
  2. RED = 'red'
  3. GREEN = 'green'
  4. BLUE = 'blue'
  5. 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.

  1. class Color(StrEnum):
  2. RED = &#39;red&#39;
  3. GREEN = &#39;green&#39;
  4. BLUE = &#39;blue&#39;
  5. 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.

  1. from enum import Enum
  2. class myenum(str, Enum):
  3. COLORRED = 'colorred'
  4. COLORBLUE = 'colorblue'
  5. mylist = [x.value for x in myenum]
  6. # 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.

  1. from enum import Enum
  2. class myenum(str, Enum):
  3. COLORRED = &#39;colorred&#39;
  4. COLORBLUE = &#39;colorblue&#39;
  5. mylist = [x.value for x in myenum]
  6. # 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:

确定