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 Facebook và Twitter để 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
- Mở tệp ở chế độ đọc và xử lý ở chế độ văn bản
- Đọc văn bản bằng hàm read[]
- 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
- Độ 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
- 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 : 60
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 61_______6_______2
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60_______6_______5
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 66
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 67
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60
len
___The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 61
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 64
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60
len
___The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 69
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 61
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 62
đầ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 : 63
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 64
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 65
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 66
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67
=
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 61
60
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60
63
64
65
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 65
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 64
68
=
len
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 61
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 62
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 65
64
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 68
Đầ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 : 63
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 64
# Quick Two Line Codes
1
# Quick Two Line Codes
2 # Quick Two Line Codes
3
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 66
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67
=
# Quick Two Line Codes
7
60
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60
63
64
65
# Quick Two Line Codes
1
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 64
68
=
len
countOfWords
9_______140_______0=
1
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 62
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 65
64
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 68
Đầ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 : 63
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 64
len
1
# Quick Two Line Codes
2 len
3
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 66
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67
=
# Quick Two Line Codes
7
60
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60
63
64
65
len
1
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 64
68
=
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 608_______6_______09
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 610
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 611
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 612
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 613
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 62
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 65
64
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 68
Đầ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 : 63
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 64
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 66
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67
=
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 61
60
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60
63
64
65
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 64
68
=
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 636
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 637
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 638
64
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 640
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 62
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 60
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 65
64
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 68
Đầ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 Codes
2
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 649
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 650
The original string is : Geeksforgeeks is best Computer Science Portal The number of words in string are : 67
=
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 61
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 654
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 655_______140_______
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 657
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 658
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 659
=
len
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 662
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 63_______6_______64
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 665
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!! The number of words in string are : 666
Đầ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ự