Python data structure and algorithm (1) -- enum type enum

Geek learning programming 2021-08-09 15:13:27
python data structure algorithm enum

Preface

The reason why bloggers think again and again , Set up a basic unpopular course of data structure . Because most books on data structures use C/C++, Undoubtedly increases the threshold of learning .

and python Language is relatively speaking , Easier to get started and master , adopt python Learning data structures and algorithms , It seems more friendly for beginners .

This article , The first is the enumeration type . stay python In the library , Provides an enumeration module enum. Through this module , We can define literal integers and strings that programmers can easily understand .

Create enumeration

Don't talk much , Let's start by creating a simple enumeration class ： week . That is, the enumeration class of the day of the week that is easy for programmers to understand . The specific definition is as follows ：

``````import enum
class Week(enum.Enum):
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
print("name", Week.Monday.name)
print("value", Week.Monday.value)
Copy code ``````

After running , The console output is as follows ：

From the code above , We know , Define an enumeration class , Must derive Enum. Also define class properties that describe values name With specific values value.

Iterative enumeration

here , Because it is the enumeration class we actively define , So we can know the specific name and value. But most of the time , We refer to enumeration classes defined by others , I don't know the specific name And value. This is the time , You need to traverse to get each member of its enumeration .

below , Let's go through the week enumeration above , The specific code is as follows ：

``````import enum
class Week(enum.Enum):
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
for i in Week:
print("name=", i.name, end=" ")
print("value=", i.value)
Copy code ``````

After running ,name and value It will show .

Comparison enumeration

Because enumeration specific values can be of any type . So enumeration types cannot compare sizes , Can only compare equality . Let's take a simple example ：

``````import enum
class Week(enum.Enum):
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
one_Week = Week.Monday
print(one_Week == Week.Monday)
print(one_Week == Week.Tuesday)
print(one_Week > Week.Tuesday)
Copy code ``````

After running , The effect is as follows ：

You can see , Compare enumeration size , Will produce TypeError abnormal .

enum.IntEnum

Of course , If you want to compare the size of the enumeration . You can also change the inherited enumeration type . For example, the week enumerations above are all integers , Then directly derived from enumerating integers . such , You can compare each other , The specific code is as follows ：

``````import enum
class Week(enum.IntEnum):
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
one_Week = Week.Monday
print(one_Week == Week.Monday)
print(one_Week == Week.Tuesday)
print(one_Week > Week.Tuesday)
Copy code ``````

After running , The effect is as follows ：

Unique enumeration value

After learning above , Some readers try to define two different in the same enumeration name Equal to the same value ？ In this case , How enumeration is handled ？

``````import enum
class Week(enum.IntEnum):
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
Mon= 1
Tue= 2
for i in Week:
print("name=", i.name, end=" ")
print("value=", i.value)
Copy code ``````

here , Bloggers in the week enumeration , Added 2 A week , And it's equal to 1,2. After running , You'll find that 2 Values are not output . This is because , Enumeration members with the same value will be considered as the alias of the previous member by default . The advantage of this is to prevent duplicate values during iterations .

If you want to define the enumeration type , No members with the same value . You can add modifiers to defined enumeration classes @unique. This is the time , If there are members with the same value , You're going to report a mistake .

``````import enum
@enum.unique
class Week(enum.IntEnum):
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
Mon= 1
Tue= 2
Copy code ``````

Create enumeration in code

In practical applications , We often don't define enumeration classes directly . But when the program needs , Dynamically generate enumeration types . This is the time , We have two ways to define ：

1. Automatically generate values of enumeration types ：
``````import enum
Week=enum.Enum(
value="Week",
names=('Monday Tuesday Wednesday Thursday Friday Saturday Sunday '),
)
for i in Week:
print("name=", i.name, end=" ")
print("value=", i.value)
Copy code ``````

here , Automatically from 1 to name Assign a value to each member variable in the ,names Member variables in are separated by spaces . The operation effect is the same as above , Not here to show .

1. Manually generate values of enumeration types ：
``````import enum
Week = enum.Enum(
value="Week",
names=[
('Monday', 1),
('Tuesday', 2),
('Wednesday', 3),
('Thursday', 4),
('Friday', 5),
('Saturday', 6),
('Sunday', 7),
],
)
for i in Week:
print("name=", i.name, end=" ")
print("value=", i.value)
Copy code ``````

This way to create , You can manually assign values to each member . Which is more convenient , It is still necessary to judge in combination with the actual situation .

https://pythonmana.com/2021/08/20210809150912547k.html