INTERVIEW QUESTION - Count the frequency of words appearing in a string Using Python
Vložit
- čas přidán 22. 01. 2020
- INTERVIEW QUESTION - Count the frequency of words appearing in a string Using Python
GitHub Link :- github.com/netsetos/python_co...
~-~~-~~~-~~-~
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)
Thanks for quality videos
Awesome video
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().split(" ")
c=dict()
for i in a:
c[i]=a.count(i)
print(c)
s = input('Enter your string')
output = { key:s.count(key) for key in s.split() }
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 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))
Thank you❤
Thank you
Thanks ..
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)
Well, if you want you may not add, "d.keys()", instead you can use only d.
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
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
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.
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)
mam thats great...please keep making this videos as it will help us alot.
details explanation before go into short way.
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)
dct = {}
st = input("")
lst = st.split()
for i in lst:
dct[i] = lst.count(i)
Very good bro 🤜
I am not able to understand this solution
yeah I really like the tutorial but what is the intro for?
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 😊
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😢