<i id='L3x1c'><tr id='L3x1c'><dt id='L3x1c'><q id='L3x1c'><span id='L3x1c'><b id='L3x1c'><form id='L3x1c'><ins id='L3x1c'></ins><ul id='L3x1c'></ul><sub id='L3x1c'></sub></form><legend id='L3x1c'></legend><bdo id='L3x1c'><pre id='L3x1c'><center id='L3x1c'></center></pre></bdo></b><th id='L3x1c'></th></span></q></dt></tr></i><div id='L3x1c'><tfoot id='L3x1c'></tfoot><dl id='L3x1c'><fieldset id='L3x1c'></fieldset></dl></div>

      1. <legend id='L3x1c'><style id='L3x1c'><dir id='L3x1c'><q id='L3x1c'></q></dir></style></legend>
        <tfoot id='L3x1c'></tfoot>

        • <bdo id='L3x1c'></bdo><ul id='L3x1c'></ul>

        <small id='L3x1c'></small><noframes id='L3x1c'>

      2. 如何比较 Python 中的枚举?

        How to compare Enums in Python?(如何比较 Python 中的枚举?)
        <i id='VNSka'><tr id='VNSka'><dt id='VNSka'><q id='VNSka'><span id='VNSka'><b id='VNSka'><form id='VNSka'><ins id='VNSka'></ins><ul id='VNSka'></ul><sub id='VNSka'></sub></form><legend id='VNSka'></legend><bdo id='VNSka'><pre id='VNSka'><center id='VNSka'></center></pre></bdo></b><th id='VNSka'></th></span></q></dt></tr></i><div id='VNSka'><tfoot id='VNSka'></tfoot><dl id='VNSka'><fieldset id='VNSka'></fieldset></dl></div>
          <legend id='VNSka'><style id='VNSka'><dir id='VNSka'><q id='VNSka'></q></dir></style></legend>

          • <bdo id='VNSka'></bdo><ul id='VNSka'></ul>

            1. <small id='VNSka'></small><noframes id='VNSka'>

                    <tbody id='VNSka'></tbody>
                • <tfoot id='VNSka'></tfoot>
                  本文介绍了如何比较 Python 中的枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  从 Python 3.4 开始,存在 Enum 类.

                  Since Python 3.4, the Enum class exists.

                  我正在编写一个程序,其中一些常量具有特定的顺序,我想知道哪种方式最适合比较它们:

                  I am writing a program, where some constants have a specific order and I wonder which way is the most pythonic to compare them:

                  class Information(Enum):
                      ValueOnly = 0
                      FirstDerivative = 1
                      SecondDerivative = 2
                  

                  现在有一种方法,需要将Information的给定information与不同的枚举进行比较:

                  Now there is a method, which needs to compare a given information of Information with the different enums:

                  information = Information.FirstDerivative
                  print(value)
                  if information >= Information.FirstDerivative:
                      print(jacobian)
                  if information >= Information.SecondDerivative:
                      print(hessian)
                  

                  直接比较不适用于枚举,所以有三种方法,我想知道哪种方法更受欢迎:

                  The direct comparison does not work with Enums, so there are three approaches and I wonder which one is preferred:

                  方法一:使用价值观:

                  if information.value >= Information.FirstDerivative.value:
                       ...
                  

                  方法 2:使用 IntEnum:

                  Approach 2: Use IntEnum:

                  class Information(IntEnum):
                      ...
                  

                  方法 3:根本不使用枚举:

                  Approach 3: Not using Enums at all:

                  class Information:
                      ValueOnly = 0
                      FirstDerivative = 1
                      SecondDerivative = 2
                  

                  每种方法都有效,方法 1 有点冗长,而方法 2 使用不推荐的 IntEnum 类,而方法 3 似乎是在添加 Enum 之前这样做的方式.

                  Each approach works, Approach 1 is a bit more verbose, while Approach 2 uses the not recommended IntEnum-class, while and Approach 3 seems to be the way one did this before Enum was added.

                  我倾向于使用方法 1,但我不确定.

                  I tend to use Approach 1, but I am not sure.

                  感谢您的建议!

                  推荐答案

                  我之前没有遇到过 Enum,所以我扫描了文档(https://docs.python.org/3/library/enum.html) ... 并找到了 OrderedEnum(第 8.13.13.2 节)这不是你想要的吗?来自文档:

                  I hadn'r encountered Enum before so I scanned the doc (https://docs.python.org/3/library/enum.html) ... and found OrderedEnum (section 8.13.13.2) Isn't this what you want? From the doc:

                  >>> class Grade(OrderedEnum):
                  ...     A = 5
                  ...     B = 4
                  ...     C = 3
                  ...     D = 2
                  ...     F = 1
                  ...
                  >>> Grade.C < Grade.A
                  True
                  

                  这篇关于如何比较 Python 中的枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)

                • <legend id='fj45a'><style id='fj45a'><dir id='fj45a'><q id='fj45a'></q></dir></style></legend>
                    <tbody id='fj45a'></tbody>
                  <tfoot id='fj45a'></tfoot>
                  • <bdo id='fj45a'></bdo><ul id='fj45a'></ul>
                  • <i id='fj45a'><tr id='fj45a'><dt id='fj45a'><q id='fj45a'><span id='fj45a'><b id='fj45a'><form id='fj45a'><ins id='fj45a'></ins><ul id='fj45a'></ul><sub id='fj45a'></sub></form><legend id='fj45a'></legend><bdo id='fj45a'><pre id='fj45a'><center id='fj45a'></center></pre></bdo></b><th id='fj45a'></th></span></q></dt></tr></i><div id='fj45a'><tfoot id='fj45a'></tfoot><dl id='fj45a'><fieldset id='fj45a'></fieldset></dl></div>

                      • <small id='fj45a'></small><noframes id='fj45a'>