INTERVIEW QUESTION - Count the frequency of words appearing in a string Using Python
HTML-код
- Опубликовано: 25 авг 2024
- INTERVIEW QUESTION - Count the frequency of words appearing in a string Using Python
GitHub Link :- github.com/net...
~-~~-~~~-~~-~
Please watch: "LRU Cache (With Python Code) "
• LRU Cache Implementati...
~-~~-~~~-~~-~
d={}
s=str(input("enter a string:" ))
sp=s.split()
for i in sp:
d[i]=sp.count(i)
print(d)
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)
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()
Yes, this one seems more elegant. Key initialization is an overkill even for beginners.
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)
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}")
this doesn't work, it prints the occurrence twice of a single word. like if 'hello' is twice:
it will show
hello : 2
hello : 2
very easy...it works
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)
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
You need to split the string before iterating.
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=", ")
Not a Pythonian way - explicit should be preferred over implicit, simple over complicted. It will work, probably. But you shouldn't nest operations without any necessity.
@@necronlord52 I disagree. The fewer the lines of code, the better. The time complexity and the space complexity of my solution would be smaller than a longer solution - my solution solves the problem quicker and uses less resources
Well, if you want you may not add, "d.keys()", instead you can use only d.
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)
a=input().split(" ")
c=dict()
for i in a:
c[i]=a.count(i)
print(c)
dct = {}
st = input("")
lst = st.split()
for i in lst:
dct[i] = lst.count(i)
Very good bro 🤜
s = input('Enter your string')
output = { key:s.count(key) for key in s.split() }
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)
Thanks for quality videos
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))
import re
from collections import Counter
s = s.strip('.')
a = list(re.split('[.
]',s))
a.remove('')
a = Counter(a)
for k,v in a.items():
print(k,v)
Awesome video
def frequency_wor():
str = input("Enter the words : ")
sp = str.split()
d = {}
for i in sp:
if i in d:
d[i] = d.get(i) + 1
else:
d[i] = 1
print(d)
frequency_wor()
Nice bro
mam thats great...please keep making this videos as it will help us alot.
It's a good tutorial. But ma'am can we consider fullstop as a word? Or can any other punctuation marks be considered as word?? please reply
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)
yeah I really like the tutorial but what is the intro for?
I am not able to understand this solution
details explanation before go into short way.
Thank you❤
Thank you
Thanks ..
How to print this without colan?? I need to print as sheela1loves2mango2.... and so on..
#Let's split this words based on space and store it in a list
Ls = Sentence.split(' ')
#remove the dot and other symbols
Found_words = []
Repeated_times = []
for word in ls:
if word not in Found_words:
Found_words.append(word)
Repeated_times.append(1)
else:
Idx= Found_words.index(word)
Repeated_times[Idx] = Repeated_times[Idx] + 1
# now accessing the added words and based on the count greater than one, we are printing the result.
For index, word in enumerate(Found_words):
if Repeated_times[index] > 1:
print(f"{Found_words[index]} founded {Repeated_times[index]}")
❤Happy coding 😊
I want to show only word, do not show ". : 2"
# .Count the frequency of words appearing in a string Using Python
def solution(string):
list = string.split()
unique_word = set(list)
dict={}
for key in unique_word:
dict[key] = list.count(key)
print(dict)
string="Sheena loves eating apple and mango. Her sister also loves eating apple and mango"
solution(string)
is this code working for anyone when iam trying to execute it not showing output
same
Same too
Yes, it did.
I don't think that kind of Questions asked in interviews...
ye question aa gya bhai, tumhari advice nhi suni, toh kr diya 😂
I've had this exact question in two interviews now
It has been asked for me in internal hiring for QA, QA is getting such questions.
then you don't watch
Very bad explanation😢