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...
    ~-~~-~~~-~~-~

Комментарии • 49

  • @safariyakm
    @safariyakm 6 месяцев назад +10

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

  • @niraja1994
    @niraja1994 12 часов назад

    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)

  • @shantanuwanare5486
    @shantanuwanare5486 3 года назад +11

    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()

    • @necronlord52
      @necronlord52 7 месяцев назад

      Yes, this one seems more elegant. Key initialization is an overkill even for beginners.

  • @deepkothari7211
    @deepkothari7211 22 дня назад +1

    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)

  • @aminemixes9304
    @aminemixes9304 3 года назад +4

    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}")

    • @learnvik
      @learnvik 2 года назад +3

      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

    • @kiranhinge22
      @kiranhinge22 Год назад

      very easy...it works

  • @techiewithcamera
    @techiewithcamera Год назад +3

    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)

  • @user-qx8xn7et5l
    @user-qx8xn7et5l 7 месяцев назад +1

    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

    • @shaktipattnayak
      @shaktipattnayak 3 месяца назад

      You need to split the string before iterating.

  • @andypandy1ify
    @andypandy1ify Год назад +2

    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=", ")

    • @necronlord52
      @necronlord52 7 месяцев назад

      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.

    • @andypandy1ify
      @andypandy1ify 7 месяцев назад

      @@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

  • @SumitKumar-ls6bq
    @SumitKumar-ls6bq 2 года назад +2

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

  • @shubhimanocha2620
    @shubhimanocha2620 13 дней назад

    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)

  • @Honey12682
    @Honey12682 6 месяцев назад

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

  • @mrpresidentrestro
    @mrpresidentrestro Год назад +1

    dct = {}
    st = input("")
    lst = st.split()
    for i in lst:
    dct[i] = lst.count(i)

  • @kamleshnizare4633
    @kamleshnizare4633 4 месяца назад

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

  • @hbTrendsIn
    @hbTrendsIn 8 месяцев назад

    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)

  • @khetrabasitechvideo362
    @khetrabasitechvideo362 2 года назад +2

    Thanks for quality videos

  • @nothymasango8331
    @nothymasango8331 9 месяцев назад

    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))

  • @hamzaahmad3387
    @hamzaahmad3387 Год назад

    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)

  • @sharmishthadevi7637
    @sharmishthadevi7637 3 года назад +2

    Awesome video

  • @shantanuwanare5486
    @shantanuwanare5486 3 года назад +3

    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()

  • @MrMukulpandey
    @MrMukulpandey 2 года назад +1

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

  • @sumedhasen4534
    @sumedhasen4534 3 года назад +1

    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

    • @hbTrendsIn
      @hbTrendsIn 8 месяцев назад

      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)

  • @PerfectBlue1412
    @PerfectBlue1412 3 года назад +2

    yeah I really like the tutorial but what is the intro for?

  • @rohittiwari7019
    @rohittiwari7019 10 месяцев назад +1

    I am not able to understand this solution

  • @ideatv2389
    @ideatv2389 8 месяцев назад

    details explanation before go into short way.

  • @pannagabm2100
    @pannagabm2100 7 месяцев назад

    Thank you❤

  • @siddhigolatkar8558
    @siddhigolatkar8558 9 месяцев назад

    Thank you

  • @shailesh__learning3848
    @shailesh__learning3848 3 года назад

    Thanks ..

  • @jekhaarchana8207
    @jekhaarchana8207 3 года назад

    How to print this without colan?? I need to print as sheela1loves2mango2.... and so on..

    • @harishparamathmananda1385
      @harishparamathmananda1385 6 месяцев назад

      #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 😊

  • @misatran1107
    @misatran1107 21 день назад

    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)

  • @-SUSHMASAI
    @-SUSHMASAI 3 года назад

    is this code working for anyone when iam trying to execute it not showing output

  • @shubhamchoudhary5461
    @shubhamchoudhary5461 3 года назад +6

    I don't think that kind of Questions asked in interviews...

    • @Lj-pk2ee
      @Lj-pk2ee 8 месяцев назад +4

      ye question aa gya bhai, tumhari advice nhi suni, toh kr diya 😂

    • @TorchwoodTurtle11
      @TorchwoodTurtle11 6 месяцев назад +3

      I've had this exact question in two interviews now

    • @chavviM
      @chavviM 5 месяцев назад +1

      It has been asked for me in internal hiring for QA, QA is getting such questions.

    • @siddarthayadav8825
      @siddarthayadav8825 2 месяца назад +1

      then you don't watch

  • @sudarshansbhat8665
    @sudarshansbhat8665 Год назад +1

    Very bad explanation😢