Chuỗi Python

Việc liệt kê trong Python được triển khai bằng cách sử dụng mô-đun có tên "enum". Bảng liệt kê được tạo bằng cách sử dụng các lớp. Enums có tên và giá trị liên quan đến chúng

Thuộc tính của enum

  • Enums có thể được hiển thị dưới dạng chuỗi hoặc repr
  • Enums có thể được kiểm tra các loại của chúng bằng cách sử dụng type[]
  • Từ khóa "tên" được sử dụng để hiển thị tên của thành viên enum

ví dụ 1. Một lớp trong Python

Mã Python để chứng minh liệt kê

Python3




from enum93_______ Enum

 

class

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
0

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
2
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
4

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
6
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
8

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
0_______0_______3
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
2

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
4_______0_______3
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
6

 

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
7

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
9

 

Enum is hashed
0

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
Enum is hashed
2

 

Enum is hashed
3

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
Enum is hashed
5

 

Enum is hashed
6

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
Enum is hashed
8
Enum is hashed
9
Dog and cat are different animals
Lions and cat are different
0

 

Dog and cat are different animals
Lions and cat are different
1

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
Enum is hashed
8
Dog and cat are different animals
Lions and cat are different
4
Dog and cat are different animals
Lions and cat are different
0

 

Dog and cat are different animals
Lions and cat are different
6

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
Enum is hashed
8
Dog and cat are different animals
Lions and cat are different
9from0

đầu ra.  

Season.SPRING
SPRING
1


[, , , ]

ví dụ 2. Chế độ tiếp cận

Thành viên có thể được truy cập theo hai cách

  • Theo giá trị. - Trong phương thức này, giá trị của thành viên enum được truyền vào
  • Bằng tên. - Trong phương thức này, tên của thành viên enum được truyền

Một giá trị hoặc tên riêng biệt cũng có thể được truy cập bằng cách sử dụng từ khóa “tên” hoặc “giá trị”

Python3




from enum93_______ Enum

 

class

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
0

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
2
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
4

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
6
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
8

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
0_______0_______3
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
2

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
4_______0_______3
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
6

 

import3

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
Enum is hashed
8import6import7
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
8import9

 

Enum0

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
Enum is hashed
8Enum3Enum4Enum5Enum6

đầu ra.  

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3

ví dụ 3. Bảng liệt kê có thể lặp lại. Chúng có thể được lặp lại bằng các vòng lặp

Trong ví dụ này, chúng ta sẽ sử dụng vòng lặp for để in tất cả các thành viên của lớp Enum.

Python3




from enum93_______ Enum

 

class

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
0

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
2
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
4

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
6
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
3
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
8

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
0_______0_______3
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
2

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
4_______0_______3
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
6

 

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
09
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
10
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
11
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
12

The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
1
1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER
8
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
15
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
16
The enum member associated with value 2 is :  SUMMER
The enum member associated with name AUTUMN is :  3
17

đầu ra.  

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER

Ví dụ 4. Bảng liệt kê hỗ trợ băm

Trong ví dụ này, chúng tôi sẽ chỉ ra cách người dùng có thể băm lớp Enum có thể được sử dụng trong từ điển hoặc bộ

Làm cách nào để sử dụng phép liệt kê trong Python?

Việc liệt kê trong Python được triển khai bằng cách sử dụng mô-đun có tên "enum". Bảng liệt kê được tạo bằng cách sử dụng các lớp. .
Enums có thể được hiển thị dưới dạng chuỗi hoặc repr
Enums có thể được kiểm tra các loại của chúng bằng cách sử dụng type[]
Từ khóa "tên" được sử dụng để hiển thị tên của thành viên enum

Tôi có nên sử dụng enums trong Python không?

Nói chung, bạn có thể sử dụng enum bất cứ khi nào bạn có một biến có thể nhận một trong số các giá trị có thể có giới hạn . Python không có kiểu dữ liệu enum như một phần cú pháp của nó. May mắn thay, Python 3. 4 đã thêm mô-đun enum vào thư viện chuẩn.

Làm cách nào để tạo một enum trong Python?

Phương thức này được gọi theo hai cách khác nhau. .
để tra cứu một thành viên hiện có. tầng lớp. Lớp enum được gọi. giá trị. Giá trị cần tra cứu
sử dụng cls enum để tạo một enum mới. tầng lớp. Lớp enum được gọi. giá trị. Tên của Enum mới để tạo. tên. Tên/giá trị của các thành viên cho Enum mới. mô-đun

Làm cách nào để truyền chuỗi thành enum?

Sử dụng Enum. Phương thức IsDefined[] để kiểm tra xem tên chuỗi hoặc giá trị số nguyên đã cho có được xác định trong một phép liệt kê đã chỉ định hay không. Do đó, việc chuyển đổi Chuỗi thành Enum có thể được thực hiện bằng cách sử dụng Enum. Phân tích cú pháp [ ] và Enum .

Chủ Đề