Kiểm tra xem chuỗi trong enum python

Trong bài viết hôm nay, bạn sẽ tìm hiểu mọi thứ bạn cần biết về enum trong Python. Enum là cách viết tắt của phép liệt kê trong Python. Trong Python, chúng tôi tạo các bảng liệt kê bằng cách sử dụng các lớp bao gồm tên [thành viên] được liên kết với các giá trị duy nhất. Các giá trị không đổi

Mục lục

🐍 Học lập trình Python
🔨 Khái niệm cơ bản về Python
👉 Cú pháp Python
👉 Biến Python
👉 Bản in Python[]
👉 Đầu vào Python[]
👉 Hằng Python
👉 Bình luận Python
⚙️ Thông tin cụ thể về Python
👉 Lọc danh sách trong Python
👉 Thay thế các mục trong danh sách bằng Python
👉 Tạo GUI bằng Python
👉 Tìm độ dài của một chuỗi trong Python
👉 Enum trong Python
👉 Câu lệnh một dòng If/Else trong Python
👉 Python xrange[]
👉 Cắt danh sách Python
🏋️ Bài tập Python
👉 Tuyển tập bài tập Python

ℹ️ Bài viết này có kho Replit tương ứng mà bạn có thể sao chép và nghịch. Tạo một tài khoản Replit miễn phí và nhấp vào nút Fork Repl bên dưới. Đây là cách tốt nhất để thực hành lập trình

phuộc thay thế

Hướng dẫn Python. Số học Python

Vui lòng bật JavaScript

Hướng dẫn Python. Các toán tử số học trong Python được giải thích bằng các ví dụ đơn giản

Enum được sử dụng để làm gì trong Python?

Enum trong Python được sử dụng để lặp lại và so sánh các tên [thành viên] mà bạn gán cho lớp. Chúng ta có thể sử dụng vòng lặp for để lặp qua tất cả các thành viên của lớp enum. Nhưng hãy lưu ý rằng trong khi chúng ta sử dụng cú pháp lớp cho enums, thì enums không được coi là lớp Python điển hình

Làm cách nào để sử dụng Enum trong Python?

Có vô số cách sử dụng Enum trong Python. Cách dễ nhất để tìm hiểu là làm việc thông qua các ví dụ dưới đây. Nói chung, lớp enum được sử dụng bằng cách nhập

from enum import Enum class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # Print enum member as string print[CarBrands.BMW] # print name of enum member using 'name' keyword print[CarBrands.BMW.name] # print value of enum member using the 'value' keyword print[CarBrands.BMW.value] # print type of enum member using the type[] function print[type[CarBrands.BMW]] # print enum member as repr print[repr[CarBrands.BMW]] # print all enum members using the "list" keyword print[list[CarBrands]]

Code language: Python [python]
8 và sau đó tạo một lớp có tham số

from enum import Enum class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # Print enum member as string print[CarBrands.BMW] # print name of enum member using 'name' keyword print[CarBrands.BMW.name] # print value of enum member using the 'value' keyword print[CarBrands.BMW.value] # print type of enum member using the type[] function print[type[CarBrands.BMW]] # print enum member as repr print[repr[CarBrands.BMW]] # print all enum members using the "list" keyword print[list[CarBrands]]

Code language: Python [python]
8 và một số thành viên enum

from enum import Enum class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5

Ví dụ Enum Python

Cách tốt nhất để hiểu cách thức hoạt động của Enum trong Python là cùng nhau xem qua một số ví dụ. Bạn có thể chơi xung quanh với nó khi chúng tôi đi

Ví dụ 1 – Lớp Python Enum

Đây là một ví dụ về lớp enum trông như thế nào và nó có thể trả về cái gì

from enum import Enum class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # Print enum member as string print[CarBrands.BMW] # print name of enum member using 'name' keyword print[CarBrands.BMW.name] # print value of enum member using the 'value' keyword print[CarBrands.BMW.value] # print type of enum member using the type[] function print[type[CarBrands.BMW]] # print enum member as repr print[repr[CarBrands.BMW]] # print all enum members using the "list" keyword print[list[CarBrands]]

Code language: Python [python]

đầu ra

Ví dụ 2 – Truy cập dữ liệu thành viên Enum

Để truy cập dữ liệu của các thành viên enum, chúng ta có thể nhắm mục tiêu theo giá trị hoặc tên

  • Sử dụng từ khóa

    class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # getting name of enum member using 'name' keyword print[f"The name of the enum member with the value of 1 is: {CarBrands[1].name}"] # getting value of enum member using the 'value' keyword print[f"The value of the enum member with the name of 'BMW' is: {CarBrands['BMW'].value}"]

    Code language: Python [python]
    0 để truy cập giá trị của thành viên
  • Sử dụng từ khóa

    class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # getting name of enum member using 'name' keyword print[f"The name of the enum member with the value of 1 is: {CarBrands[1].name}"] # getting value of enum member using the 'value' keyword print[f"The value of the enum member with the name of 'BMW' is: {CarBrands['BMW'].value}"]

    Code language: Python [python]
    1 để truy cập tên thành viên

Dưới đây là một ví dụ về cách chúng tôi sẽ sử dụng phương pháp này trên một số dữ liệu mẫu của chúng tôi

class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # getting name of enum member using 'name' keyword print[f"The name of the enum member with the value of 1 is: {CarBrands[1].name}"] # getting value of enum member using the 'value' keyword print[f"The value of the enum member with the name of 'BMW' is: {CarBrands['BMW'].value}"]

Code language: Python [python]

đầu ra

Thành viên Enum có thể gọi được. Điều này cho phép chúng tôi có được một thành viên enum bởi

class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # getting name of enum member using 'name' keyword print[f"The name of the enum member with the value of 1 is: {CarBrands[1].name}"] # getting value of enum member using the 'value' keyword print[f"The value of the enum member with the name of 'BMW' is: {CarBrands['BMW'].value}"]

Code language: Python [python]
0 của nó. Chúng ta có thể thử truy cập thương hiệu xe hơi

class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # getting name of enum member using 'name' keyword print[f"The name of the enum member with the value of 1 is: {CarBrands[1].name}"] # getting value of enum member using the 'value' keyword print[f"The value of the enum member with the name of 'BMW' is: {CarBrands['BMW'].value}"]

Code language: Python [python]
3 theo giá trị của nó

class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # getting name of enum member using 'name' keyword print[f"The name of the enum member with the value of 1 is: {CarBrands[1].name}"] # getting value of enum member using the 'value' keyword print[f"The value of the enum member with the name of 'BMW' is: {CarBrands['BMW'].value}"]

Code language: Python [python]
4

print[CarBrands[4]]

Code language: PHP [php]

đầu ra

CarBrands.TESLA

Code language: CSS [css]

Lớp Enum cũng sử dụng phương thức

class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # getting name of enum member using 'name' keyword print[f"The name of the enum member with the value of 1 is: {CarBrands[1].name}"] # getting value of enum member using the 'value' keyword print[f"The value of the enum member with the name of 'BMW' is: {CarBrands['BMW'].value}"]

Code language: Python [python]
5, mà chúng ta có thể sử dụng bằng cách sử dụng dấu ngoặc vuông để truy cập một thành viên enum theo tên của nó

Chúng tôi có thể lấy thành viên

class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # getting name of enum member using 'name' keyword print[f"The name of the enum member with the value of 1 is: {CarBrands[1].name}"] # getting value of enum member using the 'value' keyword print[f"The value of the enum member with the name of 'BMW' is: {CarBrands['BMW'].value}"]

Code language: Python [python]
3 theo tên của nó bằng cú pháp này

print[CarBrands['TESLA']]]

Code language: CSS [css]

đầu ra

CarBrands.TESLA

Code language: CSS [css]

Ví dụ 3 – Lặp lại Enums bằng vòng lặp For

Như đã đề cập ngắn gọn ở trên, chúng ta có thể lặp qua các thành viên enum bằng vòng lặp for. Điều này cho phép chúng tôi in ra tất cả các thành viên của lớp enum. Như bạn có thể thấy trong ví dụ bên dưới, chúng tôi cũng có thể bao gồm các từ khóa như

class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # getting name of enum member using 'name' keyword print[f"The name of the enum member with the value of 1 is: {CarBrands[1].name}"] # getting value of enum member using the 'value' keyword print[f"The value of the enum member with the name of 'BMW' is: {CarBrands['BMW'].value}"]

Code language: Python [python]
1 và

class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # getting name of enum member using 'name' keyword print[f"The name of the enum member with the value of 1 is: {CarBrands[1].name}"] # getting value of enum member using the 'value' keyword print[f"The value of the enum member with the name of 'BMW' is: {CarBrands['BMW'].value}"]

Code language: Python [python]
0 trong vòng lặp for để hiển thị dữ liệu theo cách có cấu trúc hơn

class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # Using a for loop to iterate through enum class members for cars in [CarBrands]: print[f"{cars.value} - {cars.name}"]

đầu ra

Ví dụ 4 – Làm việc với Hashes

Tiếp theo, chúng tôi xem xét cách chúng tôi có thể làm việc với hàm băm bằng cách sử dụng các thành viên enum của mình để có thể làm việc với từ điển và bộ. Phiên bản đầu tiên mà chúng tôi sử dụng

class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # getting name of enum member using 'name' keyword print[f"The name of the enum member with the value of 1 is: {CarBrands[1].name}"] # getting value of enum member using the 'value' keyword print[f"The value of the enum member with the name of 'BMW' is: {CarBrands['BMW'].value}"]

Code language: Python [python]
9 đang sử dụng khối if/else đơn giản để kiểm tra xem các giá trị có được băm hay không. Phiên bản thứ hai mà chúng tôi sử dụng

print[CarBrands[4]]

Code language: PHP [php]
0 đang sử dụng một hình thức ngắn hơn, thiết thực hơn

class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # Hashing enum member as dictionary our_dict = {} our_dict[CarBrands.BMW] = 'Munich' our_dict[CarBrands.AUDI] = 'Ingolstadt' # Evaluating if enum values are hashed or not if our_dict == {CarBrands.BMW: 'Munich', CarBrands.AUDI: 'Ingolstadt'}: print["Enum Member is hashed."] else: print["Enum Member is not hashed."] # Shorter version our_dict_two = {} our_dict_two == {CarBrands.BMW: 'Munich', CarBrands.AUDI: 'Ingolstadt'}

Code language: Python [python]

đầu ra

Enum Member is hashed.

Code language: plaintext [plaintext]

Ví dụ 5 – So sánh kết quả liệt kê

Chúng tôi cũng có thể so sánh kết quả liệt kê của mình bằng cách sử dụng cả hai từ khóa

print[CarBrands[4]]

Code language: PHP [php]
1 và

print[CarBrands[4]]

Code language: PHP [php]
2 cũng như các toán tử so sánh

print[CarBrands[4]]

Code language: PHP [php]
3 và

print[CarBrands[4]]

Code language: PHP [php]
4

Một ví dụ tốt sẽ là như sau

from enum import Enum class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # Print enum member as string print[CarBrands.BMW] # print name of enum member using 'name' keyword print[CarBrands.BMW.name] # print value of enum member using the 'value' keyword print[CarBrands.BMW.value] # print type of enum member using the type[] function print[type[CarBrands.BMW]] # print enum member as repr print[repr[CarBrands.BMW]] # print all enum members using the "list" keyword print[list[CarBrands]]

Code language: Python [python]
0

đầu ra

from enum import Enum class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # Print enum member as string print[CarBrands.BMW] # print name of enum member using 'name' keyword print[CarBrands.BMW.name] # print value of enum member using the 'value' keyword print[CarBrands.BMW.value] # print type of enum member using the type[] function print[type[CarBrands.BMW]] # print enum member as repr print[repr[CarBrands.BMW]] # print all enum members using the "list" keyword print[list[CarBrands]]

Code language: Python [python]
1

Ví dụ 6 – Kiểm tra xem Thành viên có mặt không

Ta có thể sử dụng từ khóa

print[CarBrands[4]]

Code language: PHP [php]
5 để kiểm tra xem một thành viên nào đó có mặt trong tập hợp thành viên hay không

from enum import Enum class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # Print enum member as string print[CarBrands.BMW] # print name of enum member using 'name' keyword print[CarBrands.BMW.name] # print value of enum member using the 'value' keyword print[CarBrands.BMW.value] # print type of enum member using the type[] function print[type[CarBrands.BMW]] # print enum member as repr print[repr[CarBrands.BMW]] # print all enum members using the "list" keyword print[list[CarBrands]]

Code language: Python [python]
2

đầu ra

from enum import Enum class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # Print enum member as string print[CarBrands.BMW] # print name of enum member using 'name' keyword print[CarBrands.BMW.name] # print value of enum member using the 'value' keyword print[CarBrands.BMW.value] # print type of enum member using the type[] function print[type[CarBrands.BMW]] # print enum member as repr print[repr[CarBrands.BMW]] # print all enum members using the "list" keyword print[list[CarBrands]]

Code language: Python [python]
3

Bảng liệt kê trong Python là bất biến

Điều này ngăn chúng tôi thêm hoặc xóa thành viên sau khi liệt kê được đặt. Điều này cũng ngăn chúng tôi thay đổi giá trị thành viên

Nếu chúng tôi muốn thêm một thành viên mới vào bảng liệt kê

print[CarBrands[4]]

Code language: PHP [php]
6, chúng tôi sẽ nhận được một

print[CarBrands[4]]

Code language: PHP [php]
7

from enum import Enum class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # Print enum member as string print[CarBrands.BMW] # print name of enum member using 'name' keyword print[CarBrands.BMW.name] # print value of enum member using the 'value' keyword print[CarBrands.BMW.value] # print type of enum member using the type[] function print[type[CarBrands.BMW]] # print enum member as repr print[repr[CarBrands.BMW]] # print all enum members using the "list" keyword print[list[CarBrands]]

Code language: Python [python]
4

đầu ra

from enum import Enum class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # Print enum member as string print[CarBrands.BMW] # print name of enum member using 'name' keyword print[CarBrands.BMW.name] # print value of enum member using the 'value' keyword print[CarBrands.BMW.value] # print type of enum member using the type[] function print[type[CarBrands.BMW]] # print enum member as repr print[repr[CarBrands.BMW]] # print all enum members using the "list" keyword print[list[CarBrands]]

Code language: Python [python]
5

Điều này cũng đúng nếu chúng ta muốn thay đổi giá trị của một thành viên enum hiện có

from enum import Enum class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # Print enum member as string print[CarBrands.BMW] # print name of enum member using 'name' keyword print[CarBrands.BMW.name] # print value of enum member using the 'value' keyword print[CarBrands.BMW.value] # print type of enum member using the type[] function print[type[CarBrands.BMW]] # print enum member as repr print[repr[CarBrands.BMW]] # print all enum members using the "list" keyword print[list[CarBrands]]

Code language: Python [python]
6

đầu ra

from enum import Enum class CarBrands[Enum]: BMW = 1 AUDI = 2 MERCEDES = 3 TESLA = 4 SKODA = 5 # Print enum member as string print[CarBrands.BMW] # print name of enum member using 'name' keyword print[CarBrands.BMW.name] # print value of enum member using the 'value' keyword print[CarBrands.BMW.value] # print type of enum member using the type[] function print[type[CarBrands.BMW]] # print enum member as repr print[repr[CarBrands.BMW]] # print all enum members using the "list" keyword print[list[CarBrands]]

Code language: Python [python]
7

Tóm lược

Hiểu enum trong Python có thể hơi khó khăn. Cách tốt nhất để học nó là làm việc thông qua một số ví dụ mà chúng tôi đã cung cấp ở trên và nghịch mã. Enum trong Python có nhiều trường hợp sử dụng khác nhau và là một hàm được sử dụng rộng rãi

Làm cách nào để so sánh chuỗi với tên enum?

Để so sánh một chuỗi với một enum, hãy mở rộng từ lớp str khi khai báo lớp liệt kê của bạn , e. g. lớp Màu [str, Enum]. . Sau đó, bạn sẽ có thể so sánh một chuỗi với một thành viên enum bằng cách sử dụng toán tử đẳng thức ==.

Làm cách nào để kiểm tra xem chuỗi có tồn tại trong enum TypeScript không?

Cách tốt nhất tôi tìm thấy là lấy tất cả Giá trị Enum dưới dạng một mảng và sử dụng indexOf trên đó .

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 .

Điều tra viên trong Python là gì?

Kiểu liệt kê làm gì trong Python? . Liệt kê trả về một đối tượng chứa bộ đếm làm khóa cho từng giá trị trong một đối tượng, làm cho các mục trong bộ sưu tập dễ dàng truy cập hơn. converts a data collection object into an enumerate object. Enumerate returns an object that contains a counter as a key for each value within an object, making items within the collection easier to access.

Chủ Đề