Bộ đếm bộ sưu tập () trong python là gì?

Trong hướng dẫn này, bạn sẽ học cách sử dụng lớp Counter của Python từ mô-đun bộ sưu tập để đếm các mục. Lớp Counter cung cấp một phương thức cực kỳ Pythonic để đếm các mục trong danh sách, bộ dữ liệu, chuỗi, v.v. Bởi vì đếm các mục là một nhiệm vụ phổ biến trong lập trình, nên có thể thực hiện việc này một cách dễ dàng và thanh lịch là một kỹ năng hữu ích cho bất kỳ Pythonista nào.

Lớp

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
6 cung cấp một lớp con cho từ điển Python, bổ sung nhiều cách hữu ích để dễ dàng đếm các mục trong một đối tượng khác. Ví dụ: bạn có thể dễ dàng trả về số lượng mục, mục phổ biến nhất và thậm chí thực hiện phép tính trên các mục
# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
6 khác nhau

Đến cuối hướng dẫn này, bạn sẽ học được

  • Cách sử dụng lớp
    # Checking Attributes of the Python Counter Class
    from collections import Counter
    
    counter = Counter[]
    
    print['Type of counter is: ',type[counter]]
    print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]
    
    # Returns:
    # Type of counter is:  
    # Counter is a subclass of a dictionary:  True
    6 để đếm các mục trong Python
  • Cách lấy các mục phổ biến nhất và ít phổ biến nhất trong một đối tượng truy cập
  • Cách cộng và trừ các đối tượng Counter khác nhau
  • cách cập nhật các đối tượng Bộ đếm trong Python

Video hướng dẫn Python Collections Counter

Mục lục

  • Hiểu lớp bộ đếm bộ sưu tập của Python
  • Tạo một đối tượng truy cập trong Python
  • Truy cập các giá trị bộ đếm trong Python
  • Tìm mục phổ biến nhất trong bộ đếm Python
  • Cập nhật giá trị bộ đếm trong Python
  • Xóa giá trị bộ đếm trong Python
  • Các phép toán số học trên các đối tượng truy cập trong Python
  • Kết hợp các đối tượng truy cập trong Python
  • Tìm từ phổ biến nhất trong chuỗi Python
  • Phần kết luận
  • Tài nguyên bổ sung

Hiểu lớp bộ đếm bộ sưu tập của Python

Lớp Python

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
6 là một phần không thể thiếu của mô-đun
# Creating Our First Counter
from collections import Counter

a_string = 'hello! welcome to datagy'
counter = Counter[a_string]

print[counter]

# Returns:
# Counter[{'e': 3, 'l': 3, 'o': 3, ' ': 3, 't': 2, 'a': 2, 'h': 1, '!': 1, 'w': 1, 'c': 1, 'm': 1, 'd': 1, 'g': 1, 'y': 1}]
0. Lớp này cung cấp các phương thức Pythonic và cực kỳ trực quan để đếm các mục trong một lần lặp, chẳng hạn như danh sách, bộ dữ liệu hoặc chuỗi. Điều này cho phép bạn đếm tần suất của các mục trong lần lặp đó, bao gồm cả việc tìm kiếm mục phổ biến nhất

Hãy bắt đầu bằng cách tạo một đối tượng

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
6 trống. Trước tiên chúng ta cần nhập lớp từ mô-đun
# Creating Our First Counter
from collections import Counter

a_string = 'hello! welcome to datagy'
counter = Counter[a_string]

print[counter]

# Returns:
# Counter[{'e': 3, 'l': 3, 'o': 3, ' ': 3, 't': 2, 'a': 2, 'h': 1, '!': 1, 'w': 1, 'c': 1, 'm': 1, 'd': 1, 'g': 1, 'y': 1}]
0. Sau đó, chúng ta có thể khởi tạo đối tượng

# Creating an Empty Counter Object
from collections import Counter

counter = Counter[]

Bây giờ chúng ta đã tạo đối tượng

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
6 đầu tiên, hãy khám phá một số thuộc tính của đối tượng. Ví dụ: chúng ta có thể kiểm tra loại của nó bằng cách sử dụng hàm
# Creating Our First Counter
from collections import Counter

a_string = 'hello! welcome to datagy'
counter = Counter[a_string]

print[counter]

# Returns:
# Counter[{'e': 3, 'l': 3, 'o': 3, ' ': 3, 't': 2, 'a': 2, 'h': 1, '!': 1, 'w': 1, 'c': 1, 'm': 1, 'd': 1, 'g': 1, 'y': 1}]
4. Chúng tôi cũng có thể xác minh rằng đối tượng là một lớp con của từ điển Python

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True

Bây giờ bạn đã hiểu về lớp

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
6 của Python, hãy bắt đầu với việc tạo đối tượng Counter đầu tiên của chúng ta

Tạo một đối tượng truy cập trong Python

Hãy tạo đối tượng Python Counter đầu tiên của chúng ta. Chúng ta có thể truyền vào một chuỗi và đối tượng

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
6 sẽ trả về số lượng của tất cả các chữ cái trong chuỗi đó

Lớp chỉ nhận một tham số duy nhất, mục chúng tôi muốn đếm. Hãy xem làm thế nào chúng ta có thể sử dụng nó

# Creating Our First Counter
from collections import Counter

a_string = 'hello! welcome to datagy'
counter = Counter[a_string]

print[counter]

# Returns:
# Counter[{'e': 3, 'l': 3, 'o': 3, ' ': 3, 't': 2, 'a': 2, 'h': 1, '!': 1, 'w': 1, 'c': 1, 'm': 1, 'd': 1, 'g': 1, 'y': 1}]

Bằng cách in bộ đếm của chúng tôi, chúng tôi có thể thấy rằng nó trả về một đối tượng giống như từ điển. Các mục được sắp xếp theo tần suất xuất hiện của từng mục trong đối tượng. Trong trường hợp này, chúng ta có thể thấy rằng chữ cái

# Creating Our First Counter
from collections import Counter

a_string = 'hello! welcome to datagy'
counter = Counter[a_string]

print[counter]

# Returns:
# Counter[{'e': 3, 'l': 3, 'o': 3, ' ': 3, 't': 2, 'a': 2, 'h': 1, '!': 1, 'w': 1, 'c': 1, 'm': 1, 'd': 1, 'g': 1, 'y': 1}]
7 tồn tại ba lần trong chuỗi của chúng ta

Truy cập các giá trị bộ đếm trong Python

Vì đối tượng Counter trả về một lớp con của từ điển, nên chúng ta có thể sử dụng các phương thức từ điển để truy cập số lượng của một mục trong từ điển đó. Hãy xem cách chúng ta có thể truy cập số lần chữ cái

# Creating Our First Counter
from collections import Counter

a_string = 'hello! welcome to datagy'
counter = Counter[a_string]

print[counter]

# Returns:
# Counter[{'e': 3, 'l': 3, 'o': 3, ' ': 3, 't': 2, 'a': 2, 'h': 1, '!': 1, 'w': 1, 'c': 1, 'm': 1, 'd': 1, 'g': 1, 'y': 1}]
8 xuất hiện trong chuỗi của chúng ta

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
6

Chúng ta có thể thấy rằng chữ cái

# Creating Our First Counter
from collections import Counter

a_string = 'hello! welcome to datagy'
counter = Counter[a_string]

print[counter]

# Returns:
# Counter[{'e': 3, 'l': 3, 'o': 3, ' ': 3, 't': 2, 'a': 2, 'h': 1, '!': 1, 'w': 1, 'c': 1, 'm': 1, 'd': 1, 'g': 1, 'y': 1}]
8 tồn tại hai lần trong chuỗi của chúng ta. Chúng tôi thậm chí có thể truy cập số lượng mục không tồn tại trong đối tượng của chúng tôi

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
8

Trong một từ điển Python bình thường, điều này sẽ tăng một

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
60. Tuy nhiên, lớp
# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
6 đã được thiết kế để ngăn chặn điều này bằng cách ghi đè hành vi mặc định

Tìm mục phổ biến nhất trong bộ đếm Python

Lớp

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
6 giúp dễ dàng tìm thấy mục phổ biến nhất trong một đối tượng nhất định. Điều này có thể được thực hiện bằng cách áp dụng phương thức
# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
63 lên đối tượng. Hãy xem cách chúng ta có thể tìm thấy mục phổ biến nhất trong đối tượng của mình

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
3

Chúng ta có thể thấy rằng điều này trả về một danh sách các bộ dữ liệu đã được sắp xếp theo thứ tự bằng cách đặt các mục phổ biến nhất trước. Do đó, chúng ta có thể truy cập mục phổ biến nhất bằng cách truy cập chỉ mục đầu tiên

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
4

Tìm mục ít phổ biến nhất trong bộ đếm Python

Tương tự, chúng ta có thể truy cập mục ít phổ biến nhất bằng cách lấy chỉ mục cuối cùng

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
5

Tìm n mục phổ biến nhất trong bộ đếm Python

Phương thức te cũng cho phép bạn chuyển vào một số nguyên chỉ trả về số mục đó. Giả sử chúng tôi muốn lấy ba mục phổ biến nhất, bạn có thể viết

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
6

Cập nhật giá trị bộ đếm trong Python

Một trong những điều tuyệt vời về Python

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
6 là các giá trị cũng có thể được cập nhật. Điều này có thể được thực hiện bằng phương pháp
# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
65. Phương thức chấp nhận một lần lặp khác, sẽ cập nhật các giá trị tại chỗ

Hãy xem cách chúng tôi có thể đếm các mục đầu tiên bằng cách sử dụng lớp

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
6 của bộ sưu tập và sau đó chuyển vào một lần lặp khác để cập nhật số lượng của chúng tôi

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
0

Chúng ta có thể thấy rằng các giá trị đã được cập nhật tại chỗ, với các giá trị của mục mới

Xóa giá trị bộ đếm trong Python

Cũng rất dễ dàng để xóa một mục khỏi đối tượng Bộ đếm. Điều này có thể hữu ích khi bạn muốn đặt lại một giá trị hoặc đơn giản là tìm cách xóa một mục khỏi tính

Bạn có thể xóa một mục khỏi đối tượng Bộ đếm bằng cách sử dụng từ khóa

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
67. Hãy tải một đối tượng Counter và sau đó xóa một giá trị

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
0

Các phép toán số học trên các đối tượng truy cập trong Python

Việc áp dụng các phép tính số học như cộng và trừ trên các đối tượng Counter cũng dễ dàng không kém. Điều này cho phép bạn kết hợp các đối tượng Bộ đếm hoặc tìm sự khác biệt giữa hai mục

Điều này có thể được thực hiện bằng cách sử dụng các toán tử

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
68 và
# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
69 tương ứng. Trước tiên hãy xem phép cộng

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
1

Bây giờ hãy trừ hai bộ đếm

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
2

Kết hợp các đối tượng truy cập trong Python

Chúng ta cũng có thể kết hợp các đối tượng Counter bằng toán tử

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
80 và
# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
81. Chúng phục vụ các mục đích rất khác nhau. Hãy phá vỡ chúng một chút

  • # Checking Attributes of the Python Counter Class
    from collections import Counter
    
    counter = Counter[]
    
    print['Type of counter is: ',type[counter]]
    print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]
    
    # Returns:
    # Type of counter is:  
    # Counter is a subclass of a dictionary:  True
    80 sẽ trả về các giá trị tối thiểu dương chung
  • # Checking Attributes of the Python Counter Class
    from collections import Counter
    
    counter = Counter[]
    
    print['Type of counter is: ',type[counter]]
    print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]
    
    # Returns:
    # Type of counter is:  
    # Counter is a subclass of a dictionary:  True
    81 sẽ trả về các giá trị cực đại dương

Trước tiên chúng ta hãy xem xét toán tử

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
80

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
3

Bây giờ chúng ta hãy xem giá trị lớn nhất giữa hai đối tượng Counter

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
4

Tìm từ phổ biến nhất trong chuỗi Python

Trước khi kết thúc hướng dẫn, chúng ta hãy xem một ví dụ thực tế. Chúng ta có thể sử dụng lớp

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
6 của Python để đếm tìm từ phổ biến nhất trong một chuỗi. Hãy tải Zen of Python và tìm từ phổ biến nhất trong chuỗi đó

Trước khi chuyển chuỗi vào lớp

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
6, chúng ta cần tách nó ra. Chúng ta có thể sử dụng phương thức
# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
87 để tách bất kỳ ký tự khoảng trắng nào, bao gồm cả dòng mới. Sau đó, chúng ta có thể áp dụng phương pháp
# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
63 và truy cập giá trị của mục đầu tiên bằng cách truy cập mục
# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
89

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
5

Phần kết luận

Trong bài đăng này, bạn đã học cách sử dụng lớp

# Checking Attributes of the Python Counter Class
from collections import Counter

counter = Counter[]

print['Type of counter is: ',type[counter]]
print['Counter is a subclass of a dictionary: ', issubclass[Counter, dict]]

# Returns:
# Type of counter is:  
# Counter is a subclass of a dictionary:  True
6 của bộ sưu tập Python. Bạn đã bắt đầu bằng cách tìm hiểu cách lớp có thể được sử dụng để tạo tần số của một đối tượng có thể lặp lại. Sau đó, bạn đã học cách tìm số đếm của một mục cụ thể và cách tìm mục có tần suất xuất hiện nhiều nhất và ít nhất. Sau đó, bạn đã học cách cập nhật số đếm, cũng như thực hiện phép tính số học trên các mục đếm này

Bộ đếm bộ sưu tập trong Python là gì?

Bộ đếm là lớp con dict dùng để đếm các đối tượng có thể băm . Nó là một bộ sưu tập nơi các phần tử được lưu trữ dưới dạng khóa từ điển và số lượng của chúng được lưu trữ dưới dạng giá trị từ điển. Số lượng được phép là bất kỳ giá trị số nguyên nào kể cả số không hoặc số âm.

Bộ đếm trả về trong Python là gì?

Python Counter là một lớp con của lớp dict hoặc dictionary. Nó theo dõi tần suất của từng phần tử trong vùng chứa. Nó nhận đối số là một đối tượng có thể lặp lại [như danh sách] và trả về từ điển .

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

Để sử dụng bộ đếm, hãy làm theo các bước bên dưới. .
Nhập bộ đếm từ mô-đun bộ sưu tập. từ bộ sưu tập nhập Counter
Tạo một thể hiện của bộ đếm. c = Bộ đếm[x].
đầu ra in. Đầu ra sẽ giống như một từ điển tôi. e. các khóa sẽ là mục và giá trị sẽ là tần suất của mục

CNT trong Python là gì?

cnt = Counter[] Bạn có thể chuyển một [danh sách] có thể lặp lại cho hàm Counter[] để tạo đối tượng bộ đếm. list = [1,2,3,4,1,2,6,7,3,8,1] Counter[list] Cuối cùng, hàm Counter[] có thể lấy từ điển làm đối số. Trong từ điển này, giá trị của khóa phải là 'số lượng' của khóa đó.

Chủ Đề