Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to find most repeated word in a string in python

from collections import Counter

# Example phrase 
phrase = "John is the son of John second. Second son of John second is William second."
split_phrase = phrase.split()

# create counter object
Counter = Counter(split_phrase)

most_occured_words = Counter.most_common(4)
  
print(most_occured_words)
Comment

most repeated character in a string python

### Has No Modules ###

# The string
string = input("What 'thing' do you want the most repeated character of? Just type it!: ")

# Setting an empty dictionary (Key = Character + Value = Frequency)
character_frequency = {}

for character in string:
  if character in character_frequency:
    character_frequency[character] += 1
  else:
    character_frequency[character] = 1

    
Values = sorted([kv[1] for kv in character_frequency.items()], reverse=True)

for k,v in character_frequency.items():
  if v == Values[0]:
    print("'" + k + "'", "is the most repeated character in '" + string + "'.")
Comment

PREVIOUS NEXT
Code Example
Python :: double for in python 
Python :: python transpose list of lists 
Python :: detect keypress in python 
Python :: remove outliers python dataframe 
Python :: input array of string in python 
Python :: read json from api python 
Python :: python get input from console 
Python :: python set remove 
Python :: how to do date time formatting with strftime in python 
Python :: django template tag multiple arguments 
Python :: convert a number column into datetime pandas 
Python :: how to add color to python text 
Python :: how to restart program in python 
Python :: pytorch freeze layers 
Python :: how do you see if a data type is an integer python 
Python :: how to set default user group in django 
Python :: pandas groupby percentile 
Python :: python3 change file permissions 
Python :: remove duplicates python 
Python :: python add all items in list 
Python :: get column number in dataframe pandas 
Python :: make averages on python 
Python :: python currency symbol 
Python :: Django group by date from datetime field 
Python :: reverse geocode python 
Python :: what is the use of class in python 
Python :: df count zeros 
Python :: copy a dict in python 
Python :: embed discord.py 
Python :: python date iso 8601 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =