Viết một hàm trong python để đếm các từ trong tệp văn bản kết thúc bằng bảng chữ cái e

Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực thi chương trình nói trên

Trình chỉnh sửa mã Python

Có một cách khác để giải quyết giải pháp này?

Trước. Viết chương trình Python để xóa các ký tự có giá trị chỉ số lẻ của một chuỗi đã cho
Kế tiếp. Viết tập lệnh Python lấy đầu vào từ người dùng và hiển thị đầu vào đó ở dạng chữ hoa và chữ thường

Mức độ khó của bài tập này là gì?

Dễ dàng trung bình khó

Kiểm tra kỹ năng Lập trình của bạn với bài kiểm tra của w3resource



Theo dõi chúng tôi trên FacebookTwitter để cập nhật thông tin mới nhất.

con trăn. Lời khuyên trong ngày

Ước chung lớn nhất

Thư viện toán học tiêu chuẩn của Python có các phương thức tuyệt vời giúp thực hiện hầu hết mọi phép tính toán cơ bản một cách dễ dàng. Đây là. phương thức gcd[] hiển thị ước chung lớn nhất

Bạn có thể đếm số từ trong một tệp văn bản bằng Python bằng cách làm theo trình tự các bước mà chúng ta sẽ thảo luận trong hướng dẫn này

Trong hướng dẫn này, chúng ta sẽ học cách đếm số từ trong tệp văn bản, sử dụng các chương trình mẫu Python

Các bước để đếm số từ trong tệp văn bản

Để đếm số từ trong một tệp văn bản, hãy làm theo các bước sau

  1. Mở tệp ở chế độ đọc và xử lý ở chế độ văn bản
  2. Đọc văn bản bằng hàm read[]
  3. Tách văn bản bằng dấu cách. Chúng tôi giả sử rằng các từ trong câu được phân tách bằng ký tự khoảng trắng
  4. Độ dài của danh sách phân chia phải bằng số lượng từ trong tệp văn bản
  5. Bạn có thể tinh chỉnh số lượng bằng cách làm sạch chuỗi trước khi tách hoặc xác thực các từ sau khi tách

ví dụ 1. Đếm Số Từ

Trong ví dụ Python này, chúng ta sẽ đọc một tệp văn bản và đếm số từ trong đó. Xét tệp văn bản sau

Tệp văn bản

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

Chương trình Python

file = open["C:\data.txt", "rt"]
data = file.read[]
words = data.split[]

print['Number of words in text file :', len[words]]

đầu ra

Number of words in text file : 14

ví dụ 2. Đếm số từ trong tệp văn bản có nhiều dòng

Trong ví dụ Python này, chúng ta sẽ đọc một tệp văn bản có nhiều dòng và đếm số từ trong đó. Xét tệp văn bản sau

Ký tự dòng mới phân tách các dòng trong tệp văn bản. Dòng mới là một ký tự khoảng trắng và khi chúng ta tách toàn bộ dữ liệu trong tệp văn bản bằng phương thức split[], tất cả các từ trong tất cả các câu đều được tách và trả về dưới dạng một danh sách

File văn bản – dữ liệu. txt

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
This is another line with some words.

Chương trình Python

file = open["C:\data.txt", "rt"]
data = file.read[]
words = data.split[]

print['Number of words in text file :', len[words]]

đầu ra

Number of words in text file : 21

Tóm lược

Trong hướng dẫn về Ví dụ Python này, chúng ta đã học cách đếm số từ trong Tệp văn bản, với sự trợ giúp của các chương trình ví dụ

Tiền xử lý dữ liệu là một nhiệm vụ quan trọng trong phân loại văn bản. Với sự xuất hiện của Python trong lĩnh vực khoa học dữ liệu, điều cần thiết là phải có một số tốc ký nhất định để chiếm thế thượng phong trong số những người khác. Bài viết này thảo luận về các cách đếm từ trong một câu, nó bắt đầu bằng các từ được phân tách bằng dấu cách nhưng cũng bao gồm các cách để có mặt các ký tự đặc biệt. Hãy thảo luận về những cách nhất định để thực hiện điều này

Phương pháp Ninja nhanh. Mã một dòng để tìm số từ trong một câu với Đầu vào tĩnh và động

Python3




# Quick Two Line Codes

countOfWords= len

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
1_______6_______2

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
3
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0_______6_______5
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
6

 

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
7

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
3
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0len___
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
1
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
3

 

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
4

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
3
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0len___
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
9
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
1
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
2

đầu ra

 

Phương pháp số 1. Sử dụng hàm tách split[] là phương pháp khá hữu ích và thường khá chung chung để lấy các từ ra khỏi danh sách, nhưng cách tiếp cận này không thành công khi chúng tôi đưa các ký tự đặc biệt vào danh sách.  

Python3




The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
3

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
4

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
5

 

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
6

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
7=
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
1

 

6
0

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
3
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0
6
3
6
4
6
5

 

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
5

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
4

6
8= len
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
1

 

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
2

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
3
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
5
6
4
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
7
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
8

Đầu ra

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6

Phương pháp #2. Sử dụng regex[findall[]] Biểu thức chính quy phải được sử dụng trong trường hợp chúng tôi yêu cầu xử lý các trường hợp dấu chấm câu hoặc ký tự đặc biệt trong chuỗi. Đây là cách tao nhã nhất mà nhiệm vụ này có thể được thực hiện.  

Thí dụ

Python3




The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
3

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
4

# Quick Two Line Codes1

# Quick Two Line Codes2 # Quick Two Line Codes3

 

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
6

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
7= # Quick Two Line Codes7

 

6
0

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
3
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0
6
3
6
4
6
5

 

# Quick Two Line Codes1

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
4

6
8= lencountOfWords9_______140_______0=1

 

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
2

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
3
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
5
6
4
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
7
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
8

Đầu ra

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6

Phương pháp #3. Sử dụng sum[] + strip[] + split[] Phương thức này thực hiện tác vụ cụ thể này mà không cần sử dụng regex. Trong phương pháp này, trước tiên chúng tôi kiểm tra tất cả các từ bao gồm tất cả các bảng chữ cái, nếu có, chúng sẽ được thêm vào tổng và sau đó trả về.  
 

Python3




The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
3

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
4

len1

# Quick Two Line Codes2 len3

 

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
6

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
7= # Quick Two Line Codes7

 

6
0

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
3
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0
6
3
6
4
6
5

 

len1

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
4

6
8=
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
08_______6_______09
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
10
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
11
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
12
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
13

 

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
2

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
3
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
5
6
4
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
7
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
8

Đầu ra

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6

Phương pháp #4. Sử dụng phương thức đếm[]

Python3




The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
3

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
4

 

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
6

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
7=
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
1

 

6
0

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
3
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0
6
3
6
4
6
5

 

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
4

6
8=
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
36
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
37
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
38
6
4
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
40

 

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
2

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
3
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
0
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
5
6
4
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
7
The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
8

Đầu ra

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6

Phương pháp #5. Sử dụng mô-đun shlex

Đây là một cách tiếp cận mới sử dụng phương thức split[] trong mô-đun shlex

Python3




# Quick Two Line Codes2

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
49

 

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
50

The original string is : Geeksforgeeks is best Computer Science Portal
The number of words in string are : 6
7=
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
1

 

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
54

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
55_______140_______
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
57

 

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
58

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
59= len
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
62

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
3_______6_______64
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
65

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The number of words in string are : 6
66

Đầu ra

6

Mô-đun shlex cung cấp bộ phân tích từ vựng cho các cú pháp giống như trình bao đơn giản. Nó có thể được sử dụng để chia một chuỗi thành một danh sách các từ trong khi tính đến dấu ngoặc kép, dấu thoát và các ký tự đặc biệt khác. Điều này làm cho nó trở thành một lựa chọn tốt để đếm các từ trong câu có thể chứa các ký tự như vậy

Ghi chú. các shlex. hàm split trả về một danh sách các từ, vì vậy bạn có thể sử dụng hàm len để đếm số từ trong danh sách. Phương pháp đếm cũng có thể được sử dụng trong danh sách để đạt được kết quả tương tự

Chủ Đề