Hướng dẫn how do you count frequency in python? - làm thế nào để bạn đếm tần số trong python?

Đưa ra một danh sách chưa được phân loại của một số yếu tố (có thể hoặc không phải là số nguyên), hãy tìm tần số của từng phần tử riêng biệt trong danh sách bằng từ điển. & Nbsp; ví dụ: & nbsp; & nbsp;
Example: 
 

Input : [1, 1, 1, 5, 5, 3, 1, 3, 3, 1,
                  4, 4, 4, 2, 2, 2, 2]
Output : 1 : 5
         2 : 4
         3 : 3
         4 : 3
         5 : 2
Explanation : Here 1 occurs 5 times, 2 
              occurs 4 times and so on...

Vấn đề có thể được giải quyết theo nhiều cách. Một cách tiếp cận đơn giản sẽ là lặp lại trong danh sách và sử dụng từng yếu tố riêng biệt của danh sách làm khóa của từ điển và lưu trữ số lượng tương ứng của khóa đó làm giá trị. Dưới đây là mã python cho phương pháp này: & nbsp;
 

Python

def CountFrequency(my_list):

    freq __ {}

    

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
1
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
2
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
3
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
5
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
6
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
7
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
3
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
9

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
0
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
1
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
222
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
5
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
6
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
7

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
0
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
1=
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4

    

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
1
{1: 5, 5: 2, 3: 3, 4: 3, 2: 4}
4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
3
{1: 5, 5: 2, 3: 3, 4: 3, 2: 4}
6

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
5
{1: 5, 5: 2, 3: 3, 4: 3, 2: 4}
8
{1: 5, 5: 2, 3: 3, 4: 3, 2: 4}
9def0def1def2

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
6 def4== def7
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
7

    CountFrequency(my_list):0=CountFrequency(my_list):2

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4CountFrequency(my_list):9CountFrequency(my_list):4CountFrequency(my_list):9CountFrequency(my_list):4    3CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4    3CountFrequency(my_list):4    3CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4freq 3CountFrequency(my_list):4freq 3CountFrequency(my_list):4freq 3CountFrequency(my_list):4freq 9CountFrequency(my_list):4freq 9CountFrequency(my_list):4freq 9CountFrequency(my_list):4freq 9=6

    =8

Output:

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2

Độ phức tạp về thời gian: O (n), trong đó n là độ dài của danh sách.O(N), where N is the length of the list.

Cách thay thế: Một cách tiếp cận khác có thể là sử dụng phương thức danh sách.count (). & Nbsp; & nbsp;An alternative approach can be to use the list.count() method. 
 

Python

def CountFrequency(my_list):

    freq __ {}

    

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
1
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
2
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
3
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
5
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
6
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
7
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
3
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
9

    

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
1
{1: 5, 5: 2, 3: 3, 4: 3, 2: 4}
4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
3
{1: 5, 5: 2, 3: 3, 4: 3, 2: 4}
6

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
5
{1: 5, 5: 2, 3: 3, 4: 3, 2: 4}
8
{1: 5, 5: 2, 3: 3, 4: 3, 2: 4}
9def0def1def2

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
6 def4== def7
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
7

    CountFrequency(my_list):0=CountFrequency(my_list):2

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4CountFrequency(my_list):9CountFrequency(my_list):4CountFrequency(my_list):9CountFrequency(my_list):4    3CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4    3CountFrequency(my_list):4    3CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4freq 3CountFrequency(my_list):4freq 3CountFrequency(my_list):4freq 3CountFrequency(my_list):4freq 9CountFrequency(my_list):4freq 9CountFrequency(my_list):4freq 9CountFrequency(my_list):4freq 9=6

    =8

Output:

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2

    CountFrequency(my_list):0=CountFrequency(my_list):2

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4CountFrequency(my_list):9CountFrequency(my_list):4CountFrequency(my_list):9CountFrequency(my_list):4    3CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4    3CountFrequency(my_list):4    3CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4freq 3CountFrequency(my_list):4freq 3CountFrequency(my_list):4freq 3CountFrequency(my_list):4freq 9CountFrequency(my_list):4freq 9CountFrequency(my_list):4freq 9CountFrequency(my_list):4freq 9=6
O(N2), where N is the length of the list. The time complexity list.count() is O(N) alone, and when used inside loop it will become O(N2). 

Độ phức tạp về thời gian: O (n), trong đó n là độ dài của danh sách.An alternative approach can be to use the dict.get() method. This makes the program much more shorter and makes understand how get method is useful instead of if…else. 
 

Python

def CountFrequency(my_list):

    freq __ {}

    

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
1
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
2
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
3
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
5
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
6
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
7
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
3
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
9

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
0
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
1
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
222
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
6 def4== def7
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
7

    CountFrequency(my_list):0=CountFrequency(my_list):2

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4CountFrequency(my_list):9CountFrequency(my_list):4CountFrequency(my_list):9CountFrequency(my_list):4    3CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4    3CountFrequency(my_list):4    3CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4freq 3CountFrequency(my_list):4freq 3CountFrequency(my_list):4freq 3CountFrequency(my_list):4freq 9CountFrequency(my_list):4freq 9CountFrequency(my_list):4freq 9CountFrequency(my_list):4freq 9=6

    

{1: 5, 5: 2, 3: 3, 4: 3, 2: 4}
8
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
63

Output:

{1: 5, 5: 2, 3: 3, 4: 3, 2: 4}

    CountFrequency(my_list):0=CountFrequency(my_list):2

 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4CountFrequency(my_list):9CountFrequency(my_list):4CountFrequency(my_list):9CountFrequency(my_list):4    3CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4    3CountFrequency(my_list):4    3CountFrequency(my_list):4
 1 :  5
 2 :  4
 3 :  3
 4 :  3
 5 :  2
4CountFrequency(my_list):4freq 3CountFrequency(my_list):4freq 3CountFrequency(my_list):4freq 3CountFrequency(my_list):4freq 9CountFrequency(my_list):4freq 9CountFrequency(my_list):4freq 9CountFrequency(my_list):4freq 9=6

Count frequencies of all elements in array in Python using collections module
 


Có chức năng tần số trong Python không?

Bạn có thể tìm thấy tần số bằng cách tính toán độ dài của mỗi danh sách.Nếu bạn đề cập đến dấu ngoặc đơn trong các câu như: "in (tần số (l, g))", thì chúng là do việc sử dụng Python 3. Nếu bạn tham khảo chúng trong "If (i . If you refer to the parenthesis in sentences such as: "print( frequency( l, g ) )", then they are due to the use of Python 3. IF you refer to them in "if ( i < len( values ) ):", then it is personal use.

Đếm () trong Python là gì?

Count () là một hàm tích hợp python trả về số lần một đối tượng xuất hiện trong danh sách.Phương thức đếm () là một trong những hàm tích hợp của Python.Nó trả về số lần một giá trị nhất định xảy ra trong một chuỗi hoặc một danh sách, như tên gọi.a Python built-in function that returns the number of times an object appears in a list. The count() method is one of Python's built-in functions. It returns the number of times a given value occurs in a string or a list, as the name implies.