INTERVIEW QUESTION - Count the frequency of words appearing in a string Using Python

preview_player
Показать описание
INTERVIEW QUESTION - Count the frequency of words appearing in a string Using Python

-~-~~-~~~-~~-~-
Please watch: "LRU Cache (With Python Code) "
-~-~~-~~~-~~-~-
Рекомендации по теме
Комментарии
Автор

d={}
s=str(input("enter a string:" ))
sp=s.split()
for i in sp:
d[i]=sp.count(i)
print(d)

safariyakm
Автор

you can just do this :
str = input("Enter a string: ")
li=str.split()
for val in li:
con=li.count(val)
print(f" {val} : {con}")

aminemixes
Автор

To count frequency of letters : -
def frequency_wor():
s = input("Enter the Letter : ")
d = {}
for i in s:
if i in d:
d[i] = d[i] + 1
else:
d[i] = 1
print(d)
frequency_wor()

shantanuwanare
Автор

My Solution:
def f_words(s):
dict={}
for i in s:
if i in dict.keys():
dict[i]+=1
else:
dict[i]=1
print(dict)

techiewithcamera
Автор

a = input('enter string:')
c={}

for i in a:
if i in c:
c[i] += 1
else:
c[i] =1
print(c)

this is working in simple way

DhanuPriya-ov
Автор

str1="Apple Mango Orange Mango Guava Guava Mango pineapple"

l1=str1.split()

d1={}

for i in l1:
if i not in d1.keys():
d1[i]=1
else:
d1[i]=d1[i]+1
print(d1)

# 2nd method
for i in l1:
d1[i]=d1.get(i, 0)+1

print(d1)

shubhimanocha
Автор

Well, if you want you may not add, "d.keys()", instead you can use only d.

SumitKumar-lsbq
Автор

def frequency_count():
text = input("enter the text ")
print(text)
lower_text = text.lower()
li = lower_text.split() #convert all string into list for wordss
dic = {}

for i in li:
if i not in dic.keys():
dic[i] = 0
dic[i] = dic[i] + 1
print(dic)

deepkothari
Автор

mam thats great...please keep making this videos as it will help us alot.

MrMukulpandey
Автор

def count_words():
str1 = input('Enter the string')
list_=str1.split()
dic_={}

for i in list_:
if i in dic_:
dic_[i]= dic_[i]+1
else:
dic_[i]=1
print(dic_)

count_words()

Funtastatic_vdz
Автор

Two liner:

def freq_words():
l=[i.strip(".").strip(".") for i in input("Enter a string: ").split()]
for w in set(l): print(f"'{w}' : {l.count(w)}", end=", ")

andypandyify
Автор

s = input('Enter your string')
output = { key:s.count(key) for key in s.split() }

kamleshnizare
Автор

a=input().split(" ")
c=dict()
for i in a:
c[i]=a.count(i)
print(c)

Honey
Автор

str = 'This is a sample string . This string contains multiple words .'

word_count = {}

for word in str.split():
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1

print(word_count)

niraja
Автор

details explanation before go into short way.

ideatv
Автор

def freq_word():
str = input("Enter string : ")
li= str.split()
dict ={}
for word in li:
dict.setdefault(word, 0)
dict[word] = dict[word] +1

print(dict)

freq_word()

anshumankumar
Автор

from collections import Counter
def freq_word():
str1 = input("Enter first string: ")
l1=str1.split()
d=Counter(l1)
print(d)
freq_word()

yashbhati
Автор

def Counting (sentence : str):
results = {}
string_list = sentence.strip().split()
for i in string_list:
results[i] = string_list.count(i)
return results
input = " Nothile loves eating mango apple, banana and aslo apple"
print( Counting(input))

nothymasango
Автор

sentence="Sheena eating mango and apple. Her sister also loves eating apple and mango"

sentences=sentence.split()

print(sentences)


for word in sentences:
counts=sentence.count(word)
print(f"Number of times ${word} appears:", counts)

pushpakgaikwad
Автор

import re

sentence = input("Enter a sentence: ")
words = re.findall(r'\b\w+\b', sentence)

word_counts = {}

for word in words:
count = sentence.count(word)
word_counts[word] = count

word_counts = {word: count for word, count in word_counts.items() if count > 1}
print(word_counts)

hbTrendsIn
welcome to shbcf.ru