Warm tip: This article is reproduced from stackoverflow.com, please click
enums python

Is there a way to compare contents of Enums in python?

发布于 2020-03-28 23:17:40

I have 2 enums as below:

class flower1(Enum):
    Jasmine = "Jasmine"
    Rose = "Rose"
    Lily = "Lily"


class flower2(Enum):
    Jasmine = "Jasmine"
    Sunflower="Sun flower"
    Lily = "Lily"

how to find if the enum values are equal and if not find diff of contents in the above two enums?

Questioner
jelat
Viewed
69
Ethan Furman 2020-02-01 01:12

Enums are directly iterable, so if you only need the difference and not which Enum has which member, you can do:

>>> set([m.name for m in flower1]) & set([m.name for m in flower2])
set(['Jasmine', 'Lily'])