Bộ đếm trong thư viện chuẩn python?

Trong Python, bạn có thể đếm tổng số phần tử trong một danh sách hoặc bộ dữ liệu bằng hàm tích hợp sẵn

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
5 và số lần xuất hiện của một phần tử bằng phương thức
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
6

Ngoài ra, lớp

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
7 của bộ sưu tập thư viện tiêu chuẩn có thể được sử dụng để đếm số lần xuất hiện của từng phần tử cùng một lúc

Bài viết này mô tả các nội dung sau

  • Đếm tổng số phần tử.
    import collections
    
    l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']
    
    c = collections.Counter(l)
    print(c)
    # Counter({'a': 4, 'c': 2, 'b': 1})
    
    print(type(c))
    # 
    
    print(issubclass(type(c), dict))
    # True
    
    5
  • Đếm số lần xuất hiện của một phần tử.
    import collections
    
    l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']
    
    c = collections.Counter(l)
    print(c)
    # Counter({'a': 4, 'c': 2, 'b': 1})
    
    print(type(c))
    # 
    
    print(issubclass(type(c), dict))
    # True
    
    6
  • Cách sử dụng
    print(c['a'])
    # 4
    
    print(c['b'])
    # 1
    
    print(c['c'])
    # 2
    
    print(c['d'])
    # 0
    
    0
  • Nhận các yếu tố phổ biến nhất.
    print(c['a'])
    # 4
    
    print(c['b'])
    # 1
    
    print(c['c'])
    # 2
    
    print(c['d'])
    # 0
    
    1
  • Đếm các phần tử duy nhất
  • Đếm các phần tử thỏa mãn điều kiện
  • Đếm số lần xuất hiện của một từ trong chuỗi
  • Đếm số lần xuất hiện của một ký tự trong chuỗi

Danh sách được sử dụng trong mã mẫu sau, nhưng bộ dữ liệu có thể được xử lý theo cách tương tự

Xem bài viết sau về cách đếm số ký tự/chuỗi con cụ thể trong một chuỗi

  • Đếm ký tự và chuỗi trong Python

Liên kết được tài trợ

Đếm tổng số phần tử. import collections l = ['a', 'a', 'a', 'a', 'b', 'c', 'c'] c = collections.Counter(l) print(c) # Counter({'a': 4, 'c': 2, 'b': 1}) print(type(c)) # print(issubclass(type(c), dict)) # True 5

Bạn có thể đếm tổng số phần tử trong danh sách bằng hàm tích hợp sẵn

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
5

  • Cách sử dụng len() trong Python

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

print(len(l))
# 7

nguồn. list_len_count. py

Đếm số lần xuất hiện của một phần tử. import collections l = ['a', 'a', 'a', 'a', 'b', 'c', 'c'] c = collections.Counter(l) print(c) # Counter({'a': 4, 'c': 2, 'b': 1}) print(type(c)) # print(issubclass(type(c), dict)) # True 6

Bạn có thể đếm số lần xuất hiện của một phần tử trong danh sách bằng phương thức

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
6

  • Các loại tích hợp - Các thao tác trình tự phổ biến — Python 3. 10. 0 tài liệu

Nếu một phần tử không tồn tại được thông qua, thì trả về

print(c['a'])
# 4

print(c['b'])
# 1

print(c['c'])
# 2

print(c['d'])
# 0
6

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
3

nguồn. list_len_count. py

print(c['a'])
# 4

print(c['b'])
# 1

print(c['c'])
# 2

print(c['d'])
# 0
0, được giải thích tiếp theo, rất hữu ích nếu bạn muốn đếm số lần xuất hiện của từng phần tử cùng một lúc

Cách sử dụng print(c['a']) # 4 print(c['b']) # 1 print(c['c']) # 2 print(c['d']) # 0 0

Lớp

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
7 được cung cấp trong bộ sưu tập thư viện tiêu chuẩn

  • bộ sưu tập. Bộ đếm — Kiểu dữ liệu vùng chứa — Python 3. 10. 0 tài liệu

Đối tượng

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
7 được tạo bằng cách chuyển danh sách tới
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
81

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
7 là một phân lớp của từ điển
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
83, có các phần tử là khóa và chúng được tính là giá trị

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True

nguồn. bộ sưu tập_bộ đếm. py

Bằng cách chỉ định một phần tử, bạn có thể nhận được số lượng của nó. Nếu một phần tử không tồn tại được chỉ định, thì trả về

print(c['a'])
# 4

print(c['b'])
# 1

print(c['c'])
# 2

print(c['d'])
# 0
6

print(c['a'])
# 4

print(c['b'])
# 1

print(c['c'])
# 2

print(c['d'])
# 0

nguồn. bộ sưu tập_bộ đếm. py

Bạn cũng có thể sử dụng các phương pháp

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
83 như
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
86,
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
87 và
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
88

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
8

nguồn. bộ sưu tập_bộ đếm. py

Các phương thức này trả về các đối tượng kiểu

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
89, v.v. Bạn có thể sử dụng chúng như hiện tại nếu bạn muốn sử dụng vòng lặp
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
00. Nếu bạn muốn chuyển đổi nó thành danh sách, hãy sử dụng
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
01

  • Lặp lại từ điển (khóa và giá trị) với vòng lặp for trong Python

Nhận các yếu tố phổ biến nhất. print(c['a']) # 4 print(c['b']) # 1 print(c['c']) # 2 print(c['d']) # 0 1

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
7 có phương thức
print(c['a'])
# 4

print(c['b'])
# 1

print(c['c'])
# 2

print(c['d'])
# 0
1 trả về danh sách các bộ dữ liệu của
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
05 được sắp xếp theo số lượng

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
0

nguồn. bộ sưu tập_bộ đếm. py

Bạn có thể lấy chỉ mục có số lần xuất hiện cao nhất bằng cách chỉ định chỉ mục là

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
06, chỉ mục có số lần xuất hiện thấp nhất là
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
07, v.v. Nếu bạn chỉ muốn lấy các phần tử hoặc chỉ số đếm, chỉ cần chỉ định chỉ mục sau nó

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
9

nguồn. bộ sưu tập_bộ đếm. py

Nếu bạn muốn sắp xếp theo thứ tự giảm dần, hãy sử dụng các lát cắt với số gia được đặt thành

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
08

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
1

nguồn. bộ sưu tập_bộ đếm. py

Nếu đối số

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
09 được chỉ định cho phương thức
print(c['a'])
# 4

print(c['b'])
# 1

print(c['c'])
# 2

print(c['d'])
# 0
1, thì chỉ các phần tử
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
09 có số lần xuất hiện nhiều nhất được trả về. Nếu nó bị bỏ qua, tất cả các phần tử được trả về

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
5

nguồn. bộ sưu tập_bộ đếm. py

Nếu bạn muốn có một danh sách các phần tử riêng biệt và số lượng của chúng được sắp xếp theo số lần xuất hiện, thay vì một bộ

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
05, bạn có thể làm như sau

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
7

nguồn. bộ sưu tập_bộ đếm. py

Nó sử dụng chức năng tích hợp sẵn

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
93 để chuyển đổi danh sách 2D (trong trường hợp này là danh sách các bộ dữ liệu) và giải nén và giải nén nó. Xem các bài viết sau để biết chi tiết

  • Hoán đổi danh sách 2D trong Python (hoán đổi hàng và cột)
  • Giải nén một bộ và liệt kê trong Python

Liên kết được tài trợ

Đếm các phần tử duy nhất

Nếu bạn muốn đếm các phần tử duy nhất trong danh sách hoặc bộ dữ liệu, hãy sử dụng

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
7 hoặc
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
95

Số phần tử trong đối tượng

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
7 bằng với số phần tử duy nhất trong danh sách ban đầu. Nó có thể thu được với
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
5

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
30

nguồn. bộ sưu tập_bộ đếm. py

Bạn cũng có thể sử dụng

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
98. Nếu bạn không cần đối tượng
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
7, việc sử dụng
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
98 sẽ dễ dàng hơn

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
98 là kiểu dữ liệu không có các phần tử trùng lặp và
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
95 trả về một đối tượng
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
98 với các giá trị duy nhất khi danh sách được truyền. Bạn có thể lấy số phần tử trong
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
98 với
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
5

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
31

nguồn. bộ sưu tập_bộ đếm. py

Để biết thêm thông tin về cách kiểm tra, loại bỏ và trích xuất các phần tử trùng lặp trong danh sách, hãy xem các bài viết sau

  • Kiểm tra xem danh sách có chứa các phần tử trùng lặp trong Python không
  • Xóa/trích xuất các phần tử trùng lặp khỏi danh sách trong Python

Đếm các phần tử thỏa mãn điều kiện

Để đếm số phần tử trong danh sách hoặc bộ đáp ứng một điều kiện nhất định, hãy sử dụng cách hiểu danh sách hoặc biểu thức trình tạo

  • Danh sách hiểu trong Python

Ví dụ: đếm số phần tử có giá trị âm cho danh sách sau

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
32

nguồn. list_len_count. py

Áp dụng biểu thức điều kiện cho từng phần tử với tính năng hiểu danh sách sẽ trả về một danh sách có các phần tử thuộc loại

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
16 (
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
17,
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
18)

Kiểu boolean

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
16 là một lớp con của kiểu số nguyên
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
50.
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
17 được coi là
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
52 và
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
18 là
print(c['a'])
# 4

print(c['b'])
# 1

print(c['c'])
# 2

print(c['d'])
# 0
6. Bạn có thể đếm số
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
17 (số phần tử thỏa mãn điều kiện) bằng
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
56

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
33

nguồn. list_len_count. py

Nếu

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
57 trong phần hiểu danh sách được thay thế bằng
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
58, nó sẽ trở thành biểu thức trình tạo. Khi một biểu thức trình tạo là đối số duy nhất, có thể bỏ qua
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
58

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
34

nguồn. list_len_count. py

Sử dụng

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
70 nếu bạn muốn đếm số lượng của
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
18 (số phần tử không thỏa mãn điều kiện)

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
35

nguồn. list_len_count. py

Tất nhiên, bạn có thể thay đổi các điều kiện

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
36

nguồn. list_len_count. py

Một số ví dụ khác được hiển thị dưới đây

Đếm số lượng phần tử lẻ cho một danh sách các số

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
37

nguồn. list_len_count. py

Ví dụ cho một danh sách các chuỗi

  • Trích xuất và thay thế các phần tử đáp ứng điều kiện của danh sách chuỗi trong Python

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
38

nguồn. list_len_count. py

Sử dụng

print(c['a'])
# 4

print(c['b'])
# 1

print(c['c'])
# 2

print(c['d'])
# 0
0 để đặt số lần xuất hiện làm điều kiện

Sau đây là một ví dụ về trích xuất các phần tử có hai lần xuất hiện trở lên và đếm tổng số phần tử của chúng. Trong ví dụ này, có bốn

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
73 và hai
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
74, vì vậy tổng cộng có sáu

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
39

nguồn. bộ sưu tập_bộ đếm. py

Sau đây là một ví dụ về trích xuất các giá trị của các phần tử có hai hoặc nhiều lần xuất hiện và đếm số lượng của chúng. Trong ví dụ này, có hai giá trị,

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
73 và
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
74

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
0

nguồn. bộ sưu tập_bộ đếm. py

Đếm số lần xuất hiện của một từ trong chuỗi

Ví dụ cụ thể, hãy đếm số lần xuất hiện của từ trong một chuỗi

Đầu tiên,

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
77 và
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
78 không cần thiết được thay thế bằng một chuỗi trống bằng cách sử dụng phương thức
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
79 và bị xóa. Sau đó, sử dụng phương pháp
import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
300 để tạo danh sách được phân tách bằng dấu cách

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
1

nguồn. bộ sưu tập_bộ đếm. py

Nếu bạn tạo một danh sách, bạn có thể nhận được số lần xuất hiện, v.v. như trong các ví dụ trước

import collections

l = ['a', 'a', 'a', 'a', 'b', 'c', 'c']

c = collections.Counter(l)
print(c)
# Counter({'a': 4, 'c': 2, 'b': 1})

print(type(c))
# 

print(issubclass(type(c), dict))
# True
2

nguồn. bộ sưu tập_bộ đếm. py

Trên đây là một quy trình rất đơn giản, vì vậy để xử lý ngôn ngữ tự nhiên phức tạp hơn, tốt hơn là sử dụng một thư viện như NLTK

Thư viện nào là bộ đếm trong Python?

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.

Counter() Python là gì?

Counter là một lớp con của dict được thiết kế đặc biệt để đếm các đối tượng có thể băm trong Python . Đó là một từ điển lưu trữ các đối tượng dưới dạng khóa và được tính là giá trị. Để đếm với Counter , bạn thường cung cấp một chuỗi hoặc có thể lặp lại các đối tượng có thể băm làm đối số cho hàm tạo của lớp.

Bộ đếm nhập trong Python là gì?

Bộ đếm Python là vùng chứa sẽ chứa số lượng của từng phần tử có trong vùng chứa . Bộ đếm là một lớp con có sẵn bên trong lớp từ điển. Bộ đếm là một lớp con có sẵn bên trong lớp từ điển.

Bộ đếm Python có được đặt hàng không?

Bộ đếm Python . Số phần tử bộ đếm có thể là số nguyên dương, 0 hoặc âm. Tuy nhiên, không có giới hạn nào đối với các khóa và giá trị của nó. an unordered collection where elements are stored as Dict keys and their count as dict value. Counter elements count can be positive, zero or negative integers. However there is no restriction on it's keys and values.