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
6Ngoà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úcBài viết này mô tả các nội dung sau
- Đếm tổng số phần tử.
5import 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
- Đếm số lần xuất hiện của một phần tử.
6import 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
- Cách sử dụng
0print[c['a']] # 4 print[c['b']] # 1 print[c['c']] # 2 print[c['d']] # 0
- Nhận các yếu tố phổ biến nhất.
1print[c['a']] # 4 print[c['b']] # 1 print[c['c']] # 2 print[c['d']] # 0
- Đế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
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
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
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
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
6import 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
3nguồ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úcCách sử dụng print[c['a']]
# 4
print[c['b']]
# 1
print[c['c']]
# 2
print[c['d']]
# 0
0
print[c['a']]
# 4
print[c['b']]
# 1
print[c['c']]
# 2
print[c['d']]
# 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
81import 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
6print[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
88import 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
8nguồ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
print[c['a']]
# 4
print[c['b']]
# 1
print[c['c']]
# 2
print[c['d']]
# 0
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ượngimport 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
0nguồ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
9nguồ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
08import 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
1nguồ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
5nguồ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ư sauimport 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
7nguồ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
95Số 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
5import 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
30nguồ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ơnimport 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
5import 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
31nguồ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
32nguồ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
56import 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
33nguồ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
58import 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
34nguồ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
35nguồ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
36nguồ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
37nguồ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
38nguồ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ệnSau đâ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áuimport 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
39nguồ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
74import 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
0nguồ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áchimport 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
1nguồ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
2nguồ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