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.
2. Initialize a count variable to 0.
3. Split the string using space as the reference and store the words in a list.
4. Use a for loop to traverse through the words in the list and use an if statement to check if the word in the list matches the word given by the user and increment the count.
5. Print the total count of the variable.
6. Exit.

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.

string=raw_input("Enter string:")
word=raw_input("Enter word:")
a=[]
count=0
a=string.split(" ")
for i in range(0,len(a)):
      if(word==a[i]):
            count=count+1
print("Count of the word is:")
print(count)

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.
2. The count variable is initialized to zero.
3. The string is split into words using space as the reference and stored in a list.
4. The for loop is used to traverse through the words in the list.
5. The count is incremented each time the word in the list is equal to the word given by the user.
6. The total count of the word is printed.

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:

  • Nhận Giấy chứng nhận miễn phí trong chương trình Python
  • Tham gia cuộc thi chứng nhận lập trình Python
  • Trở thành một người xếp hạng hàng đầu trong chương trình Python
  • Thực hiện các bài kiểm tra lập trình Python
  • Các bài kiểm tra thực hành theo chương: Chương 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10
  • Các bài kiểm tra giả chương: Chương 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10,

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

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

text =

 
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
0
 
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
1
 
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
2
 
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
3
 
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
4
 
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
5

 
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
6=
 
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
8
 
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
9

Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
0
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
1
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 strawberry
3

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
1=
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
4
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
1=
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
1

Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
4
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
3=
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
5
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
6
 
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
5

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
0
Occurrences of the word apple: 2
0
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
2
Occurrences of the word apple: 2
2

Occurrences of the word apple: 2
3
Occurrences of the word apple: 2
4
Occurrences of the word apple: 2
0
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
2
Occurrences of the word apple: 2
7

Occurrences of the word apple: 2
8
Occurrences of the word apple: 2
9=
Occurrences of the word apple: 2
9
Total Number of Words: 15
2
Total Number of Words: 15
3

Occurrences of the word apple: 2
3
Total Number of Words: 15
5
Total Number of Words: 15
6

Occurrences of the word apple: 2
8
Occurrences of the word apple: 2
9=
Total Number of Words: 15
3

Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
0
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 strawberry
2
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 strawberry
4
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 : 1
0

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 file

Trong 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.

Python3

Occurrences of the word apple: 2
0____9
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
3

mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
4=
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
6

mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
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:
2
0
 
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
1text 0
 
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
3text 2text 3

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
0
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
1
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
2 text 8

Occurrences of the word apple: 2
3
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
3= =2

Occurrences of the word apple: 2
3
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
0 =5
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
2
Occurrences of the word apple: 2
2

Occurrences of the word apple: 2
8
Occurrences of the word apple: 2
4
 
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
00==
 
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
03

 
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
04
 
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
05=
 
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
05
Total Number of Words: 15
2
Total Number of Words: 15
3

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 file

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.

Python3

mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
4=
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
6

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
0
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
1
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
2 text 8

Occurrences of the word apple: 2
3
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
3= =2

Occurrences of the word apple: 2
3
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
0 =5
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
2
Occurrences of the word apple: 2
2

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:
2
49

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:

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:
2
19=
 
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
0
 
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
1
 
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
2
 
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
3
 
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
4
 
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
5

Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
0
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
1
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
2 text 8

Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
4
Occurrences of the word apple: 2
0=
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
5
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
6
 
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
5

Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
4
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
4
Total Number of Words: 15
22____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:
2
41
 
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
42

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:
2
1
 
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
45
Total Number of Words: 15
2
 
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
47
 
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
48

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:
2
50
 
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
51

text =

 
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
0
 
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
1
 
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
2
 
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
3
 
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
4
 
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
5

 
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
6=
 
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
8
 
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
9

Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
0
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
1
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 strawberry
3

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
1=
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
7

Occurrences of the word apple: 2
3
Total Number of Words: 15
5
Total Number of Words: 15
6

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
1=
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
1

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
1=
 
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
79
 
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
80
 
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
81

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.
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 : 1
6
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
0

Output:

mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1