Hướng dẫn how do you count instances of text in python? - làm thế nào để bạn đếm các trường hợp văn bản trong python?

Nội dung

  • Giới thiệu
  • Cú pháp - đếm ()
  • Ví dụ 1: Đếm số lần một từ xảy ra trong tệp văn bản đã cho
  • Bản tóm tắt

Để đếm số lần xuất hiện của một từ cụ thể trong tệp văn bản, hãy đọc nội dung của tệp văn bản vào một chuỗi và sử dụng hàm chuỗi.count () với từ được truyền làm đối số cho hàm đếm ().

Cú pháp - đếm ()

Ví dụ 1: Đếm số lần một từ xảy ra trong tệp văn bản đã cho

n = String.count(word)

Bản tóm tắt

Ví dụ 1: Đếm số lần một từ xảy ra trong tệp văn bản đã cho

Bản tóm tắt

Để đếm số lần xuất hiện của một từ cụ thể trong tệp văn bản, hãy đọc nội dung của tệp văn bản vào một chuỗi và sử dụng hàm chuỗi.count () với từ được truyền làm đối số cho hàm đếm ().

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.

Sau đây là cú pháp của hàm đếm ().

#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)

trong đó

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
5 là chuỗi và
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6 trả về số lần xuất hiện của
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
5 trong chuỗi này.

Number of occurrences of the word : 2

Bản tóm tắt

Để đếm số lần xuất hiện của một từ cụ thể trong tệp văn bản, hãy đọc nội dung của tệp văn bản vào một chuỗi và sử dụng hàm chuỗi.count () với từ được truyền làm đối số cho hàm đếm ().

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức Count () chuỗi Python với sự trợ giúp của các ví dụ.

Phương thức

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6 trả về số lần xuất hiện của chuỗi con trong chuỗi đã cho.

Thí dụ

message = 'python is popular programming language'

# number of occurrence of 'p' print('Number of occurrence of p:', message.count('p'))

# Output: Number of occurrence of p: 4


Cú pháp của số lượng chuỗi

Cú pháp của phương pháp

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6 là:

string.count(substring, start=..., end=...)

Count () tham số

Phương thức

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6 chỉ yêu cầu một tham số duy nhất để thực thi. Tuy nhiên, nó cũng có hai tham số tùy chọn:

  • Chất nền - chuỗi có số lượng sẽ được tìm thấy. - string whose count is to be found.
  • Bắt đầu (Tùy chọn) - Chỉ mục bắt đầu trong chuỗi nơi tìm kiếm bắt đầu. - starting index within the string where search starts.
  • Kết thúc (Tùy chọn) - Chỉ mục kết thúc trong chuỗi nơi tìm kiếm kết thúc. - ending index within the string where search ends.

Lưu ý: Chỉ mục trong Python bắt đầu từ 0, không phải 1. Index in Python starts from 0, not 1.


Đếm () Giá trị trả về

Phương thức

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6 Trả về số lần xuất hiện của chuỗi con trong chuỗi đã cho.


Ví dụ 1: Số lượng xuất hiện của một chuỗi con đã cho

# define string
string = "Python is awesome, isn't it?"
substring = "is"

count = string.count(substring)

# print count print("The count is:", count)

Đầu ra

The count is: 2

Ví dụ 2: Số lần xuất hiện của một chuỗi con nhất định bằng cách sử dụng bắt đầu và kết thúc

# define string
string = "Python is awesome, isn't it?"
substring = "i"

# count after first 'i' and before the last 'i'

count = string.count(substring, 8, 25)

# print count print("The count is:", count)

Đầu ra

The count is: 1

Ví dụ 2: Số lần xuất hiện của một chuỗi con nhất định bằng cách sử dụng bắt đầu và kết thúc

Ở đây, việc đếm bắt đầu sau khi

#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
2 đầu tiên gặp phải, tức là vị trí chỉ số
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
3.

Đưa ra một chuỗi, tác vụ là đếm tần số của một ký tự trong chuỗi đó. Hoạt động cụ thể trên chuỗi này khá hữu ích trong nhiều ứng dụng như loại bỏ các bản sao hoặc phát hiện các ký tự không mong muốn.

Phương pháp số 1: Phương pháp ngây thơ Naive method

Lặp lại toàn bộ chuỗi cho ký tự cụ thể đó và sau đó tăng bộ đếm khi chúng ta gặp ký tự cụ thể.

#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
6
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
7
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
8

#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
9
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
7
Number of occurrences of the word : 2
1

Number of occurrences of the word : 2
2
Number of occurrences of the word : 2
3
Number of occurrences of the word : 2
4
Number of occurrences of the word : 2
5

Number of occurrences of the word : 2
6
Number of occurrences of the word : 2
7
Number of occurrences of the word : 2
3
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
7
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
7
message = 'python is popular programming language'

# number of occurrence of 'p' print('Number of occurrence of p:', message.count('p'))

# Output: Number of occurrence of p: 4
1
message = 'python is popular programming language'

# number of occurrence of 'p' print('Number of occurrence of p:', message.count('p'))

# Output: Number of occurrence of p: 4
2

message = 'python is popular programming language'

# number of occurrence of 'p' print('Number of occurrence of p:', message.count('p'))

# Output: Number of occurrence of p: 4
3
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
9
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
7
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
9
message = 'python is popular programming language'

# number of occurrence of 'p' print('Number of occurrence of p:', message.count('p'))

# Output: Number of occurrence of p: 4
7
message = 'python is popular programming language'

# number of occurrence of 'p' print('Number of occurrence of p:', message.count('p'))

# Output: Number of occurrence of p: 4
8

message = 'python is popular programming language'

# number of occurrence of 'p' print('Number of occurrence of p:', message.count('p'))

# Output: Number of occurrence of p: 4
9
string.count(substring, start=..., end=...)
0
string.count(substring, start=..., end=...)
1

________ 52 ________ 47 & nbsp;

string.count(substring, start=..., end=...)
4
string.count(substring, start=..., end=...)
5

Đầu ra:

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
0

Phương pháp số 2: Sử dụng

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6 Using
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6

Sử dụng

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6 là phương pháp thông thường nhất trong Python để có được sự xuất hiện của bất kỳ yếu tố nào trong bất kỳ container nào. Điều này dễ dàng để viết mã và ghi nhớ và do đó khá phổ biến.

#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
6
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
7
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
8

# define string
string = "Python is awesome, isn't it?"
substring = "is"

count = string.count(substring)

# print count print("The count is:", count)
1
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
7
# define string
string = "Python is awesome, isn't it?"
substring = "is"

count = string.count(substring)

# print count print("The count is:", count)
3
message = 'python is popular programming language'

# number of occurrence of 'p' print('Number of occurrence of p:', message.count('p'))

# Output: Number of occurrence of p: 4
1
# define string
string = "Python is awesome, isn't it?"
substring = "is"

count = string.count(substring)

# print count print("The count is:", count)
5

message = 'python is popular programming language'

# number of occurrence of 'p' print('Number of occurrence of p:', message.count('p'))

# Output: Number of occurrence of p: 4
9
string.count(substring, start=..., end=...)
0
string.count(substring, start=..., end=...)
1

________ 52 ________ 47 & nbsp;

string.count(substring, start=..., end=...)
4
string.count(substring, start=..., end=...)
5

Đầu ra:

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1

Phương pháp số 2: Sử dụng

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6 Using
The count is: 2
3

Sử dụng

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6 là phương pháp thông thường nhất trong Python để có được sự xuất hiện của bất kỳ yếu tố nào trong bất kỳ container nào. Điều này dễ dàng để viết mã và ghi nhớ và do đó khá phổ biến.

# define string
string = "Python is awesome, isn't it?"
substring = "is"

count = string.count(substring)

# print count print("The count is:", count)
1
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
7
# define string
string = "Python is awesome, isn't it?"
substring = "is"

count = string.count(substring)

# print count print("The count is:", count)
3
message = 'python is popular programming language'

# number of occurrence of 'p' print('Number of occurrence of p:', message.count('p'))

# Output: Number of occurrence of p: 4
1
# define string
string = "Python is awesome, isn't it?"
substring = "is"

count = string.count(substring)

# print count print("The count is:", count)
5

#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
6
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
7
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
8

________ 69 ________ 47 & nbsp;

string.count(substring, start=..., end=...)
4
The count is: 2
2

message = 'python is popular programming language'

# number of occurrence of 'p' print('Number of occurrence of p:', message.count('p'))

# Output: Number of occurrence of p: 4
9
string.count(substring, start=..., end=...)
0
string.count(substring, start=..., end=...)
1

________ 52 ________ 47 & nbsp;

string.count(substring, start=..., end=...)
4
string.count(substring, start=..., end=...)
5

Đầu ra:

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1

Phương pháp số 2: Sử dụng

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6 Using lambda +
The count is: 1
3 +
The count is: 1
4

Sử dụng

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6 là phương pháp thông thường nhất trong Python để có được sự xuất hiện của bất kỳ yếu tố nào trong bất kỳ container nào. Điều này dễ dàng để viết mã và ghi nhớ và do đó khá phổ biến.

#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
6
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
7
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
8

# define string
string = "Python is awesome, isn't it?"
substring = "is"

count = string.count(substring)

# print count print("The count is:", count)
1
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
7
# define string
string = "Python is awesome, isn't it?"
substring = "is"

count = string.count(substring)

# print count print("The count is:", count)
3
message = 'python is popular programming language'

# number of occurrence of 'p' print('Number of occurrence of p:', message.count('p'))

# Output: Number of occurrence of p: 4
1
# define string
string = "Python is awesome, isn't it?"
substring = "is"

count = string.count(substring)

# print count print("The count is:", count)
5

message = 'python is popular programming language'

# number of occurrence of 'p' print('Number of occurrence of p:', message.count('p'))

# Output: Number of occurrence of p: 4
9
string.count(substring, start=..., end=...)
0
string.count(substring, start=..., end=...)
1

________ 52 ________ 47 & nbsp;

string.count(substring, start=..., end=...)
4
string.count(substring, start=..., end=...)
5

Đầu ra:

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1

Phương pháp số 2: Sử dụng

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6
Method #5 : Using
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
25

Sử dụng

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6 là phương pháp thông thường nhất trong Python để có được sự xuất hiện của bất kỳ yếu tố nào trong bất kỳ container nào. Điều này dễ dàng để viết mã và ghi nhớ và do đó khá phổ biến.

# define string
string = "Python is awesome, isn't it?"
substring = "is"

count = string.count(substring)

# print count print("The count is:", count)
1
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
7
# define string
string = "Python is awesome, isn't it?"
substring = "is"

count = string.count(substring)

# print count print("The count is:", count)
3
message = 'python is popular programming language'

# number of occurrence of 'p' print('Number of occurrence of p:', message.count('p'))

# Output: Number of occurrence of p: 4
1
# define string
string = "Python is awesome, isn't it?"
substring = "is"

count = string.count(substring)

# print count print("The count is:", count)
5

#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
6
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
7
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

#read content of file to string
data = file.read()

#get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)
8

________ 69 ________ 47 & nbsp;

string.count(substring, start=..., end=...)
4
The count is: 2
2

message = 'python is popular programming language'

# number of occurrence of 'p' print('Number of occurrence of p:', message.count('p'))

# Output: Number of occurrence of p: 4
9
string.count(substring, start=..., end=...)
0
string.count(substring, start=..., end=...)
1

________ 52 ________ 47 & nbsp;

string.count(substring, start=..., end=...)
4
string.count(substring, start=..., end=...)
5

Đầu ra:

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1

Làm thế nào để bạn đếm các sự xuất hiện của một chuỗi trong Python?

Python String Count () Phương thức đếm () trả về số lần xuất hiện của một chuỗi con trong chuỗi đã cho.count() The count() method returns the number of occurrences of a substring in the given string.

Làm thế nào để bạn đếm các trường hợp của một từ trong Python?

Cách "tiêu chuẩn" (không có thư viện bên ngoài) để có được số lượng từ xuất hiện trong danh sách là bằng cách sử dụng hàm số () của đối tượng danh sách. Phương thức Count () là một hàm tích hợp lấy một phần tử làm đối số duy nhất của nó và trả về số lần phần tử xuất hiện trong danh sách.using the list object's count() function. The count() method is a built-in function that takes an element as its only argument and returns the number of times that element appears in the list.

Làm thế nào để bạn đếm các trường hợp giá trị trong Python?

Toán tử.countof () được sử dụng để đếm số lần xuất hiện của B trong a.Nó đếm số lần xuất hiện của giá trị.Nó trả về số lượng của một số lần xuất hiện của giá trị. countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value. It returns the Count of a number of occurrences of value.

Làm thế nào để bạn đếm các chữ cái lặp đi lặp lại trong Python?

Python..
chuỗi = "Trách nhiệm lớn" ;.
in ("ký tự trùng lặp trong một chuỗi đã cho:") ;.
#Count mỗi ký tự có trong chuỗi ..
cho i trong phạm vi (0, len (chuỗi)):.
Đếm = 1 ;.
cho J trong phạm vi (i+1, len (chuỗi)):.
if (chuỗi [i] == chuỗi [j] và chuỗi [i]! = ''):.
Đếm = đếm + 1 ;.