Hướng dẫn python program to count the occurrences of each word in a given string sentence - chương trình python để đếm số lần xuất hiện của từng từ trong một câu chuỗi đã cho
Đây là một chương trình Python để đếm các lần xuất hiện của mỗi từ trong một câu chuỗi nhất định. Mô tả vấn đề Chương trình lấy một chuỗi và tính sự xuất hiện của từng từ trong câu đã cho. Giải pháp vấn đề 1. Lấy một chuỗi và một từ từ người dùng và lưu trữ nó trong các biến riêng biệt. 2. Khởi tạo biến đếm thành 0. 3. Chia chuỗi bằng cách sử dụng không gian làm tham chiếu và lưu trữ các từ trong danh sách. 4. Sử dụng một vòng lặp để đi qua các từ trong danh sách và sử dụng câu lệnh IF để kiểm tra xem từ trong danh sách có khớp với từ được người dùng đưa ra và tăng số lượng không. 5. In tổng số của biến. 6. Thoát. Chương trình/mã nguồn Dưới đây là mã nguồn của chương trình Python để tính độ dài của chuỗi mà không cần sử dụng các hàm thư viện. Đầu ra chương trình cũng được hiển thị dưới đây. Giải thích chương trình 1. Người dùng phải nhập một chuỗi và một từ và lưu trữ nó trong các biến riêng biệt. 2. Biến số được khởi tạo về 0. 3. Chuỗi được chia thành các từ sử dụng không gian làm tham chiếu và lưu trữ trong danh sách. 4. Vòng lặp For được sử dụng để đi qua các từ trong danh sách. 5. Đếm được tăng lên mỗi lần từ trong danh sách bằng từ được đưa ra bởi người dùng. 6. Tổng số lượng của từ được in. Trường hợp kiểm tra thời gian chạy Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 2 Sê -ri Giáo dục & Học tập toàn cầu Sanfoundry - Chương trình Python. Để thực hành tất cả các chương trình Python, đây là bộ hoàn thành hơn 150 vấn đề và giải pháp Python. Bước tiếp theo:
Manish Bhojasia, một cựu chiến binh công nghệ với hơn 20 năm @ Cisco & Wipro, là người sáng lập và CTO tại Sanfoundry. Ông sống ở Bangalore, và tập trung vào sự phát triển của nhân Linux, Công nghệ San, Cvanced C, Cấu trúc dữ liệu & Alogrithms. Giữ kết nối với anh ta tại LinkedIn.Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn. Đăng ký các lớp học chính miễn phí của mình tại YouTube & Thảo luận kỹ thuật tại Telegram SanfoundryClasses. Ví dụ 2: đếm các từ của các từ cụ thể trong một tệp văn bản đã cho Tệp mẫu.txt Đầu tiên, chúng tôi tạo một tệp văn bản trong đó chúng tôi muốn đếm các từ trong Python. Đặt tệp này được mẫu.txt với các nội dung sausample.txt with the following contents Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry Ví dụ 1: đếm các lần xuất hiện của mỗi từ trong một tệp văn bản đã cho Count occurrences of each word in a given text fileỞ đây, chúng tôi sử dụng một vòng python để đọc từng dòng và từ dòng đó, chúng tôi đang chuyển đổi từng dòng thành thấp hơn cho số lượng duy nhất và sau đó chia từng từ để đếm số của nó. Python3
Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 20 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 21 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 22 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 23 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 24 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 25 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 26 = Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 28 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 29 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry0 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry1 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry2 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry3 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry4 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry1 = Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry7 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry4 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry1 = mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 11 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry4 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 13 = mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 15 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 16 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 25 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry4 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry0 Occurrences of the word apple: 20 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry2 Occurrences of the word apple: 22 Occurrences of the word apple: 23 Occurrences of the word apple: 24 Occurrences of the word apple: 20 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry2 Occurrences of the word apple: 27 Occurrences of the word apple: 28 Occurrences of the word apple: 29 = Occurrences of the word apple: 29 Total Number of Words: 152 Total Number of Words: 153 Occurrences of the word apple: 23 Total Number of Words: 155 Total Number of Words: 156 Occurrences of the word apple: 28 Occurrences of the word apple: 29 = Total Number of Words: 153 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry0 Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry.2 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry2 Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry.4 Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry.5 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry4 Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry.7 Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry.8 Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry.9 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 10 Output: mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 1 Ví dụ 2: đếm các từ của các từ cụ thể trong một tệp văn bản đã cho 2: Count occurrences of specific words in a given text fileTrong ví dụ này, chúng tôi sẽ đếm số lượng táo có mặt trong tệp văn bản. Python3Occurrences of the word apple: 20____9 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 13 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 14 = mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 16 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 17 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 20 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 21 text 0Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 23 text 2text 3Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry4 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry0 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry1 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry2 text 8Occurrences of the word apple: 23 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 13 = = 2Occurrences of the word apple: 23 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry0 = 5Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry2 Occurrences of the word apple: 22 Occurrences of the word apple: 28 Occurrences of the word apple: 24 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 200 = = Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 203 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 204 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 205 = Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 205 Total Number of Words: 152 Total Number of Words: 153 Các Output: Occurrences of the word apple: 2 Ví dụ 3: Đếm tổng số xuất hiện của các từ trong một tệp văn bản đã cho 3: Count total occurrences of words in a given text fileTrong ví dụ này, chúng tôi sẽ đếm tổng số từ có trong một tệp văn bản. Python3mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 14 = mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 16 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry4 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry0 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry1 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry2 text 8Occurrences of the word apple: 23 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 13 = = 2Occurrences of the word apple: 23 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry0 = 5Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry2 Occurrences of the word apple: 22 Các Ví dụ 3: Đếm tổng số xuất hiện của các từ trong một tệp văn bản đã cho Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 249 Output: Total Number of Words: 15 Trong ví dụ này, chúng tôi sẽ đếm tổng số từ có trong một tệp văn bản.Sample.txt: Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry. Code: Python3Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 219 = Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 20 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 21 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 22 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 23 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 24 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 25 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry0 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry1 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry2 text 8Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry4 Occurrences of the word apple: 20 = mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 15 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 16 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 25 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry4 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 14 Total Number of Words: 1522____9 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 241 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 242 Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry.7 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 21 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 245 Total Number of Words: 152 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 247 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 248 Xem xét các tệp có dấu câu Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 250 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 251
Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 20 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 21 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 22 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 23 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 24 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 25 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 26 = Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 28 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 29 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry0 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry1 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry2 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry3 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry4 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry1 = Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry7 Occurrences of the word apple: 23 Total Number of Words: 155 Total Number of Words: 156 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry4 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry1 = mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 11 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry4 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry1 = Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 279 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 280 Case 1: Enter string:hello world Enter word:hello Count of the word is: 1 Case 2: Enter string:orange blue red orange Enter word:orange Count of the word is: 281 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry4 Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry.7 Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry.8 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 16 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 10 Output: mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 1 |