- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Second most repeated word in a sequence in Python?
The string is given, our task is to find out the second repeated word. Here we Counter(iterator) for creating dictionary which contains word as key and its frequency as value.
Algorithm
Step 1: Create user define list. Step 2: Then convert list into a dictionary. Step 2: Next get the values and sort them in descending order. Step 3: Then the second element is the second largest value. Step 4: Next again traverse whole dictionary and display key whose value is equal to second largest element.
Example code
# To print Second most repeated word in a sequence in Python from collections import Counter defsecondrepeatation(A): # Convert list into dictionary con = Counter(A) res = sorted(con.values(), reverse=True) maxi = res[1] for (key, val) in con.items(): if val == maxi: print("Second most repeated word ::>",key) return # Driver program if __name__ == "__main__": A=list() #create user defined list n=int(input("Enter the size of the List ::")) print("Enter the word ::") for i in range(int(n)): k=input("") A.append(k) secondrepeatation(A) # call function
Output
Enter the size of the List ::4 Enter the word :: aa bb aa cc Second most repeated word ::> bb
- Related Articles
- Find the second most repeated word in a sequence in Java
- C++ program to find Second most repeated word in a sequence
- How to find Second most repeated string in a sequence in android?
- How to Find the Most Repeated Word in a Text File using Python?
- Find the first repeated word in a string in Python?
- Find the first repeated word in a string in Python using Dictionary
- Which is your second most favorite 'F' word?
- Write a program in Python to find the most repeated element in a series
- Find the first repeated word in a string in Java
- Find the first repeated word in a string in C++
- Write a program in Python to print the most frequently repeated element in a series
- Python program for most frequent word in Strings List
- How to Identify Most Frequently Occurring Items in a Sequence with Python?
- Finding second smallest word in a string - JavaScript
- Program to find length longest prefix sequence of a word array in Python

Advertisements